根据匹配内容将值附加到文件末尾

根据匹配内容将值附加到文件末尾

我的脚本如下

###BEGIN#
#!/bin/bash
### Daily Volume Growth
echo "Volume Name     Total Size   `date +%F`" >> volgrow

echo "`ssh 192.168.1.2 df -h Volume1 |head -2 | tail -1 | awk '{ print $1,$2 }'`" >> volgrow
echo "`ssh 192.168.1.2 df -h Volume2 |head -2 | tail -1 | awk '{ print $1,$2 }'`" >> volgrow
echo "`ssh 192.168.1.2 df -h Volume3 |head -2 | tail -1 | awk '{ print $1,$2 }'`" >> volgrow
###END#

我的输出如下

Volume Name     Total Size 2017-05-25
/vol/Volume1/ 5632GB
/vol/Volume2/ 2136GB
/vol/Volume3/ 5110GB

我的要求如下

Volume Name     Total Size 2017-05-25  2017-05-26 2017-05-27
/vol/Volume1/ 5632GB  5633GB 5630GB
/vol/Volume2/ 2136GB  2137GB 2138GB
/vol/Volume3/ 5110GB 5109GB 5111GB

答案1

###BEGIN#
#!/bin/bash
### Daily Volume Growth
#### Will execute once when file not exist and add first entry in the file
if [ ! -f volgrow ]; then
      echo "Volume Name     Total Size   `date +%F`" >> volgrow
    echo "`ssh 192.168.1.2 df -h Volume1 |head -2 | tail -1 | awk '{ 
print $1,$2 }'`" >> volgrow
    echo "`ssh 192.168.1.2 df -h Volume2 |head -2 | tail -1 | awk '{ 
print $1,$2 }'`" >> volgrow
    echo "`ssh 192.168.1.2 df -h Volume3 |head -2 | tail -1 | awk '{ 
print $1,$2 }'`" >> volgrow
fi

#### get all data in different variables 
line1 = echo "`date +%F`"
line2 = "`ssh 192.168.1.2 df -h Volume1 |head -2 | tail -1 | awk '{ print $1,$2 }'`   "
line3 = "`ssh 192.168.1.2 df -h Volume2 |head -2 | tail -1 | awk '{ print $1,$2 }'`   "
line4 = "`ssh 192.168.1.2 df -h Volume3 |head -2 | tail -1 | awk '{ print $1,$2 }'`   "

###########  append variable value in line 1, 2, 3, 4
sed -i -e "1s%$%\t$line1%" volgrow
sed -i -e "2s%$%$line2%" volgrow
sed -i -e "3s%$%$line3%" volgrow
sed -i -e "4s%$%$line4%" volgrow
###END#

相关内容