请教shell读写XML问题
现有 123.xml文件,内容是:<?xml version="1.0" encoding="GBK"?>
<videoinfo>
<videoname>呵呵</videoname>
<videopath>20080110</videopath>
<videosize>0.0</videosize>
<widthOfImage>320</widthOfImage>
<heightOfImage>240</heightOfImage>
<kbps>260</kbps>
<logo>260</logo>
<videodesc>呵呵恶化和</videodesc>
<state>1</state>
<uploadtime>Thu Jan 10 16:00:53 CST 2008</uploadtime>
</videoinfo>
我想用shell 脚本编写一个程序,修改里面的内容。比如修改<state>1</state>为<state>2</state> ,
修改<videosize>0.0</videosize>为<videosize>333</videosize> 该怎么操作呢。谢谢各位高手。 eg
[code]
awk '{if(/state/){sub(/>[^<]*</,">2<")}print}' urfile
sed 's//(<state>/)[^<]*/(<//state>/)//12/2/' urfile
[/code] 楼上的好厉害,只是看的不太明白 。能大概给解释一下吗?
awk '{if(/state/){sub(/>[^<]*</,">2<")}print}' urfile
sed 's//(<state>/)[^<]*/(<//state>/)//12/2/' urfile 这两行是都可以实现上述操作吗?我第一个测试可以,第2个测试没成功。 [quote]awk '{if(/state/){sub(/>[^<]*</,">2<")}print}' urfile
[/quote]
我也刚弄完xml的,帮你解释一下这个:
if(/state/): 匹配state行才执行后面的操作
sub(/>[^<]*</,">2<")
=sub(/正则/,"b"),用b替换正则匹配的内容
其中的 />[^<]*</ 为:
匹配 >...< 的意思
[^<]* 及不是<的多个字符
所以这句话就是:匹配以<开头,直到第一个> ! 首先感谢光明使者,精灵你们二位,哈哈。
<state>1</state>
awk '{if(/state/){sub(/>[^<]*</,">2<")}print}' urfile
这条命令难理解的就是/>[^<]*</这里了。我理解的是,匹配〉〈这个之间的内容,但是为什么[^<],看精灵解释说不是<的多个字符,但是〉〈里面已经没有〈这个符号了,为什么还要排除呢。 搞不懂。 <?xml version="1.0" encoding="UTF-8"?>
<address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="add.xsd">
<group groupname="default">
<entry>
<id>0001</id>
<name>wds</name>
<persioninfo>
<age>24</age>
<work>student</work>
<birtyday>1984</birtyday>
<qq>125153052</qq>
</persioninfo>
<connect>
<address>neusoft institute of informaction </address>
<tel>
<office>84835207</office>
</tel>
<email href="mailto:7717060@163.com"/>
</connect>
</entry>
</group>
</address>
#!/bin/bash
#-----------------------------------------------------------------------------------
# filename: main.sh |
# author: wds |
# begin: 2008.1.1 |
# end: 2008.1.4 |
# version: v.1 |
#-----------------------------------------------------------------------------------
xmladd=add.xml
xmltemp=.temp2
tim=$(date)
count=$(more $xmladd | wc -l)
function exitt()
{
rm -rf $xmltemp
exit
}
function memo()
{
clear
echo
echo "----------------------------------------------------------------------------------------------"
echo "| **************************** |"
echo "| * This is XML Homework * |"
echo "| * Author: wds * |"
echo "| * Address List * |"
echo "| **************************** |"
echo "| |"
echo "| 1.Show Informaction |"
echo "| 2.Add Informaction |"
echo "| 3.Del Informaction |"
echo "| 4.Change Infromaction |"
echo "| 5.Select Infromcation |"
echo "| 6.Help |"
echo " 7.Exit |"
echo "| |"
echo "______________________________________________________________________________________________"
}
function add()
{
clear
echo "please input your informaction!"
echo -n "id: -->" ; read id
echo -n "name:-->"; read nam
echo -n "age: -->"; read age
echo -n "work:-->"; read work
echo -n "birthday[year-month-day]: -->" ; read birthday
echo -n "qq: -->"; read qq
echo -n "address: -->>" ; read address
echo -n "office: -->>" ; read office
echo -n "email: -->>" ; read mail
echo " " >> $xmladd
echo "<entry>" >> $xmladd
echo "<id>$id</id>" >> $xmladd
echo "<name>$name</name>" >> $xmladd
echo "<persion info>" >>$xmladd
echo "<age>$age</age>" >> $xmladd
echo "<work>$work</work>"
echo "<birthday>$birthday</birthday>" >> $xmladd
echo "<qq>$qq</qq>" >>$xmladd
echo "</persion info>" >> $xmladd
echo "<connect>" >> $xmladd
echo "<address>$address</address>" >> $xmladd
echo "<tel>" >>$xmladd
echo "<office>$office</office>" >> $xmladd
echo "</tel>" >> $xmladd
echo "<email href="mailto:$mail">" >> $xmladd
echo "</connect>" >>$xmladd
echo "</entry>" >>$xmladd
}
function error()
{
clear
echo "sorry please input [1-6]:"
sleep 2
memo
}
#-------------------------------------------------------------------------------------------------
function show()
{
echo "--------------------------------------------------------------------------------------------"
cat $xmladd
echo "--------------------------------------------------------------------------------------------"
echo "Now is $tim page have $count line:"
echo "--------------------------------------------------------------------------------------------"
echo "Do you want save your informcation(y/Y)"
echo -n "-->"; read answer
if [ "$answer" = "y" -a "$answer" = "Y" ]
then
echo "</group>" >> $xmladd
echo "</address>" >> $xmladd
else
continue
fi
}
function del()
{
echo "please input id:"
echo -n "id: -->"; read id
delnum=$(more $xmladd | grep -n "$id" | cut -d: -f1)
echo "$delnum" >> ww
delnum=$(expr $delnum - 1 )
pageup=$delnum
echo "$pageup" >>ww
delnum=$(expr $delnum + 16 )
pagedown=$delnum
echo "$pagedown" >>ww
cat $xmladd | sed "$pageup,"$pagedown"d" > .temp
more .temp > $xmladd
rm -rf .temp
echo "delete OK!!!"
sleep 1
}
function change()
echo "Please Input Id:"
echo -n "id: -->"; read id
delnum=$(more $xmladd | grep -n "$id" | cut -d: -f1)
delnum=$( expr $delnum - 1 )
pageup=$delnum
delnum=$( expr $delnum + 15 )
pagedown=$delnum
cat $xmladd | sed -n "$pageup,"$pagedown"p"
echo -n "New id: -->" ; read id
echo -n "New name:-->"; read name
echo -n "New age: -->"; read age
echo -n "New work:-->"; read work
echo -n "New birthday[year-month-day]: -->" ; read birthday
echo -n "New qq: -->"; read qq
echo -n "New address: -->>" ; read address
echo -n "New office: -->>" ; read office
echo -n "New email: -->>" ; read mail
echo "<entry>" >> $xmltemp
echo "<id>$id</id>" >> $xmltemp
echo "<name>$name</name>" >> $xmltemp
echo "<persion info>" >>$xmltemp
echo "<age>$age</age>" >> $xmltemp
echo "<work>$work</work>" >> $xmltemp
echo "<birthday>$birthday</birthday>" >> $xmltemp
echo "<qq>$qq</qq>" >>$xmltemp
echo "</persion info>" >> $xmltemp
echo "<connect>" >> $xmltemp
echo "<address>$address</address>" >> $xmltemp
echo "<tel>" >>$xmltemp
echo "<office>$office</office>" >> $xmltemp
echo "</tel>" >> $xmltemp
echo "<email href="mailto:$mail">" >> $xmltemp
echo "</connect>" >>$xmltemp
echo "</entry>" >>$xmltemp
cat $xmladd | sed "$pageup,"$pagedown"d" > .temp
more .temp > $xmladd
more $xmltemp >> $xmladd
}
function selectt()
{
while :
do
echo "Please Input Id:"
echo -n "id: -->"; read id
delnum=$( expr $delnum - 1 )
pageup=$delnum
delnum=$( expr $delnum + 15 )
pagedown=$delnum
cat $xmladd | sed -n "$pageup,"$pagedown"p"
echo -n "Back enter (e/E) continue select (*)"; read answer
if [ "$answer" = "E" -o "$answer" = "e" ]
then
return memo
else
continue
fi
done
}
function help()
{
while :
do
clear
echo "
1.show: funciton show it's can list all the informaction of xml.
2.add: function add it's can insert into your need informcation to the xml.
3.del: function del it's can delete informaction from you xml.
4.change: function change if xml have some informcation you dont need you can change use this function.
5.select: function select if your xml have a lots of informcation you can use this function to select your need informcaction.
6.help: fucntion help is help your how to use this software
7.exit: quit
"
echo -n "Go back to then memo!(Y/y)"; read answer
if [ "$answer" = "y" -o "$answer" = "Y" ]
then
return memo
else
continue
fi
done
}
while :
do
memo
echo "please input your choice:"
read choice
case "$choice"
in
1 ) show ;;
2 ) add ;;
3 ) del ;;
4 ) change ;;
5 ) selectt;;
6 ) help ;;
7 ) exitt ;;
* ) error;;
esac
done
[[i] 本帖最后由 7717060 于 2008-1-10 22:12 编辑 [/i]] [quote]原帖由 [i]hyhappy[/i] 于 2008-1-10 21:31 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=7835443&ptid=1040744][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
首先感谢光明使者,精灵你们二位,哈哈。
1
awk '{if(/state/){sub(/>[^ [/quote]
/>[^<]*/</ 匹配> 开始任意个非<字符,并以<结束
为什么要用[^<]来匹配>...<这样形式的内容,又涉及到正则表达式的最长匹配原则
如果用/>.*</来匹配,那么如果遇到 <>1<>2<>这样的字符串,将匹配最长的>..<
即1<>2为匹配内容,而不是你需要的1作为匹配内容,所以/>[^<]*</将确保匹配第一个><中间的内容