當前位置:九游会j9娱乐平台-九游ag登录中心网址 » 編程軟體 » shell腳本xml文件

shell腳本xml文件-九游会j9娱乐平台

發布時間: 2024-06-24 17:06:42

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、行號r2
((r2=r1-1))
4、
行號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

熱點內容
phpjava交互 發布:2024-07-17 16:58:57 瀏覽:356
resin下jsp不能正常編譯 發布:2024-07-17 16:34:44 瀏覽:229
sqlserver如何切換主備伺服器 發布:2024-07-17 16:23:02 瀏覽:299
mc18伺服器ip 發布:2024-07-17 16:23:02 瀏覽:379
仙境傳說手游腳本 發布:2024-07-17 16:09:24 瀏覽:691
matlab命令窗口和新建腳本 發布:2024-07-17 15:51:26 瀏覽:375
建ftp文件夾 發布:2024-07-17 15:51:26 瀏覽:955
魔獸撿物腳本 發布:2024-07-17 15:27:56 瀏覽:130
開發ip伺服器 發布:2024-07-17 15:24:42 瀏覽:388
安卓系統視頻製作哪個好用 發布:2024-07-17 15:10:47 瀏覽:210
网站地图