我有以下一组 XML 数据。我想补充一下分隔符当主记录结束时。
<Record contentId="501" levelId="2" parentId="0">
<Record contentId="51873" levelId="361" parentId="0">
<Field id="27277" abc="e0f89f0c-6eed-4092-81b2-f8551df81998" type="4">
</Record>
<Field id="15584" abc="007ead3c-d1f9-47e2-aaac-c99553fc2a54" type="4">
</Record>
<Record contentId="5021427" parentId="0">
<Record contentId="5099659" parentId="0">
<Field id="27257" abc="b5eab609-5b3d-44b0-b64b-0eee0fe00951" type="6">5099659</Field>
</Record>
</Record>
我想将其更改为
<Record contentId="501" levelId="2" parentId="0">
<Record contentId="51873" levelId="361" parentId="0">
<Field id="27277" abc="e0f89f0c-6eed-4092-81b2-f8551df81998" type="4">
</Record>
<Field id="15584" abc="007ead3c-d1f9-47e2-aaac-c99553fc2a54" type="4">
</Record>@@@@
<Record contentId="5021427" parentId="0">
<Record contentId="5099659" parentId="0">
<Field id="27257" abc="b5eab609-5b3d-44b0-b64b-0eee0fe00951" type="6">5099659</Field>
</Record>
</Record>
命令我使用 sed 命令作为
sed '/^<\/Record>$/{$!{ N; s/^<\/Record>\n <Record/<\/Record>@@@@\n <Record/;ty;P;D;:y}}'
答案1
感谢您提出的宝贵意见。我可以使用下面的脚本解决我的问题。
for j in `cat -n test.xml |grep '<Record '|awk '{print $1-1}'`; do for i in `cat -n test.xml |grep '</Record>' |awk '{print $1}'`; do if [ $i -eq $j ]; then sed -i ""$i"s/<\/Record>/<\/Record>@@@@/" test.xml; fi; done; done