2、首先确定BB.Name所在行,如果有重复,需要增加head -1
r1=`grep -n "BB.Name" a.xml|awk -F: '{print $1}'|head -1`
3、<property>行号r2
((r2=r1-1))
4、</property>行号r3
((r3=r1+3))
5、删除r2和r3中间所有行
sed -i '${r2},${r3}d' a.xml
sed -i '5,8d' a.xml
sed -i '5d' a.xml
6、在r4行处读入s.txt内容
((r4=r1-2))
sed -i '$r4 r s.txt' a.xml
7、实例
#!/bin/sh
r1=`grep -n "BB.Name" a.xml|awk -F: '{print $1}'|head -1`
((r2=r1-1))
((r3=r1+3))
((r4=r1-2))
sed -i "${r2},${r3}d" a.xml
sed -i "${r4} r s.txt" a.xml
$cat test.sh#!/bin/bash
if [ -z $1 ]then
echo 'USAGE:COMMAND FILENAME'
exit 0
fi
filename=record.txt
HOST=(`sed -n 's/.*>\(.*\)<\/host>/\1/p' $1`)
OIDG=(`sed -n 's/.*>\(.*\)<\/oidgroupname>/\1/p' $1`)
COMM=(`sed -n 's/.*>\(.*\)<\/communitystring>/\1/p' $1`)
DESC=(`sed -n 's/.*>\(.*\)<\/description>/\1/p' $1`)
FILE=(`ls -l $filename >/dev/null 2>&1 | awk '{print $8}'`)
if [ ! -z $FILE ]then
echo -e "host\t\toidgroupname\t\tcomm\t\tdesc" >$filename
fi
for((i=0i<${#HOST[@]}i++))do
echo -e "${HOST[i]}\t${OIDG[i]}\t${COMM[i]}\t\t${DESC[i]}" >>$filename
done
$./test.sh file
$cat record.txt
hostoidgroupnamecommdesc
192.168.1.1 CpuUtilization_MF public 192.168.1.1_CPUUtilizaton
192.168.1.2 CpuUtilization_MF public 192.168.1.2_CPUUtilizaton
192.168.1.3 CpuUtilization_MF public 192.168.1.3_CPUUtilizaton
192.168.1.4 CpuUtilization_MF public 192.168.1.4_CPUUtilizaton
192.168.1.5 CpuUtilization_MF public 192.168.1.5_CPUUtilizaton
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)