`<?xml version="1.0" encoding="UTF-8"?>
<StudentInfo Version="1">
<Student>
<StudentId>INS469</StudentId>
<ClassId>21</ClassId>
<Amount>100</Amount>
<Location>AA</Location>
</Student>'
'<?xml version="1.0" encoding="UTF-8"?>
<StudentInfo Version="1">
<Student>
<StudentId>INS469</StudentId>
<ClassId>21</ClassId>
<Amount>100</Amount>
<Location>AA</Location>
</Student>'
'<?xml version="1.0" encoding="UTF-8"?>
<StudentInfo Version="1">
<Student>
<StudentId>INS469</StudentId>
<ClassId>21</ClassId>
<Amount>100</Amount>
<Location>AA</Location>
</Student>`
这里我想使用shell脚本更改Amount的值,我只想要唯一的值。我怎样才能做到这一点 。请建议。
答案 0 :(得分:1)
[sgeorge@sgeorge-ld staCK]$ cat xml
<?xml version="1.0" encoding="UTF-8"?>
<StudentInfo Version="1">
<Student>
<StudentId>INS469</StudentId>
<ClassId>21</ClassId>
<Amount>100</Amount>
<Location>AA</Location>
</Student>'
'<?xml version="1.0" encoding="UTF-8"?>
<StudentInfo Version="1">
<Student>
<StudentId>INS469</StudentId>
<ClassId>21</ClassId>
<Amount>100</Amount>
<Location>AA</Location>
</Student>'
'<?xml version="1.0" encoding="UTF-8"?>
<StudentInfo Version="1">
<Student>
<StudentId>INS469</StudentId>
<ClassId>21</ClassId>
<Amount>100</Amount>
<Location>AA</Location>
</Student>
[sgeorge@sgeorge-ld staCK]$ OLDIFS=$IFS; IFS=$'\n'; COUNT=1; for i in `cat xml` ; do echo $i | sed "s/<Amount>.*<\/Amount>/<Amount>"$COUNT"<\/Amount>/g" && ((COUNT++)) ; done ; IFS=$OLDIFS
<?xml version="1.0" encoding="UTF-8"?>
<StudentInfo Version="1">
<Student>
<StudentId>INS469</StudentId>
<ClassId>21</ClassId>
<Amount>6</Amount>
<Location>AA</Location>
</Student>'
'<?xml version="1.0" encoding="UTF-8"?>
<StudentInfo Version="1">
<Student>
<StudentId>INS469</StudentId>
<ClassId>21</ClassId>
<Amount>14</Amount>
<Location>AA</Location>
</Student>'
'<?xml version="1.0" encoding="UTF-8"?>
<StudentInfo Version="1">
<Student>
<StudentId>INS469</StudentId>
<ClassId>21</ClassId>
<Amount>22</Amount>
<Location>AA</Location>
</Student>
OR(如果您想要Amount的真实自定义值),请执行以下操作:
[sgeorge@sgeorge-ld staCK]$ cat amount.txt
121213424525
1213125435
1313145357460
988783784332
82990190231932
[sgeorge@sgeorge-ld staCK]$ OLDIFS=$IFS; IFS=$'\n'; COUNT=1; for i in `cat xml` ; do echo $i | grep '<Amount>' >/dev/null && AMT=$(tail -1 amount.txt) && sed -i "/^$AMT$/d" amount.txt && echo $i | sed "s/<Amount>.*<\/Amount>/<Amount>"$AMT"<\/Amount>/g" || echo $i ; done ; IFS=$OLDIFS
<?xml version="1.0" encoding="UTF-8"?>
<StudentInfo Version="1">
<Student>
<StudentId>INS469</StudentId>
<ClassId>21</ClassId>
<Amount>82990190231932</Amount>
<Location>AA</Location>
</Student>'
'<?xml version="1.0" encoding="UTF-8"?>
<StudentInfo Version="1">
<Student>
<StudentId>INS469</StudentId>
<ClassId>21</ClassId>
<Amount>988783784332</Amount>
<Location>AA</Location>
</Student>'
'<?xml version="1.0" encoding="UTF-8"?>
<StudentInfo Version="1">
<Student>
<StudentId>INS469</StudentId>
<ClassId>21</ClassId>
<Amount>1313145357460</Amount>
<Location>AA</Location>
</Student>
修改强>
OLDIFS=$IFS; IFS=$'\n'; COUNT=1; for i in `cat xml` ; do echo $i | grep '<Amount>' >/dev/null && AMT=$(tail -1 amount.txt) && sed -i "/^$AMT$/d" amount.txt && echo $i | sed "s/<Amount>.*<\/Amount>/<Amount>"$AMT"<\/Amount>/g" || echo $i ; done > /tmp/xml_output.xml; IFS=$OLDIFS
答案 1 :(得分:1)
如果&#34; ...&#34;标签在一行上,您可以使用以下awk单行(要更改的xml文本存储在&#34;文件&#34;):
awk -v AMOUNT=1 '/<Amount>.*<\/Amount>/ { print "<Amount>" AMOUNT++ "</Amount>" ; next } ; { print }' file
从&#34; amount_file.txt&#34;:
中读取值awk -v AMOUNT_FILE="amounts_file.txt" '/<Amount>.*<\/Amount>/ { getline AMOUNT < AMOUNT_FILE ; print "<Amount>" AMOUNT++ "</Amount>" ; next } ; { print }' file
在金额文件结束后,金额的值将从文件的最后一个值增加。