shell脚本xml文件
Ⅰ linux Shell Sed 命令 -- 如何 处理 XML 文件
1、思路,sed可以删除指定行内容,也可以在指定行添加内容
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
Ⅱ 姹傚姪shell鑴氭湰淇鏀箈ml鏂囦欢闂棰
#!/bin/sh
read name
read ip
sed -r -i "/name=\"$name\"/ s/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/$ip/g" file.xml
镊宸卞埗瀹氶渶瑕佹浛鎹㈢殑浣岖疆鍙婇渶瑕佹浛鎹㈢殑鍊
鎸変綘涓婇溃绀轰緥镄勯渶姹傚彲浠ュ备笅璧嫔
鍦╮ead name澶勭粰name璧嫔糰aa锛宺ead ip澶勮祴鍊14.14.14.14
Ⅲ shell鑴氭湰 镐庝箞銮峰彇xml鍐呭圭殑镙囩惧睘镐у硷纻
a=`awk '{for(i=1;i<=NF;i++)printf $i"\n";printf "\n"}' aa | grep "name=" |awk -F= '{print $2}'`;echo $a
b=`awk '{for(i=1;i<=NF;i++)printf $i"\n";printf "\n"}' aa | grep "context=" |awk -F= '{print $2}'`;echo $b
c=`awk '{for(i=1;i<=NF;i++)printf $i"\n";printf "\n"}' aa | grep "version=" |awk -F= '{print $2}'`;echo $c
杩欓噷链変袱涓獀ersion
Ⅳ 如何利用Shell脚本解析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=0;i<${#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
host oidgroupname comm desc
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