以下是示例XML文件。我需要将第三个标记(AAA
)的值替换为第五个标记。
<head>
<firsttag>
<secondtag>
<thirdtag>AAA</thirdtag>
<a><thirdtag>1111</thirdtag></a>
<fifithtag>A123</fifthtag>
</secondtag>
</firsttag>
...
...
<firsttag>
<secondtag>
<thirdtag>AAA</thirdtag>
<a><thirdtag>1111</thirdtag></a>
<fifithtag>A123</fifthtag>
</secondtag>
</firsttag>
</head>
输出应为
<head>
<firsttag>
<secondtag>
<thirdtag>AAA</thirdtag>
<a><thirdtag>1111</thirdtag></a>
<fifithtag>AAA</fifthtag>
</secondtag>
</firsttag>
...
...
<firsttag>
<secondtag>
<thirdtag>AAA</thirdtag>
<a><thirdtag>1111</thirdtag></a>
<fifithtag>AAA</fifthtag>
</secondtag>
</firsttag>
</head>
答案 0 :(得分:0)
我不确定我是否已完全理解你的问题,但这里有一个awk脚本,它看起来像是你所要求的。它不是100%健壮的,因为你实际上需要一些能够真正解析XML的东西。
awk -f this.script&lt; inputfile&gt; OUTPUTFILE
$0 ~ "<secondtag>" {
S = 1
}
$0 ~ "</secondtag>" {
S = 0
}
$0 ~ "<thirdtag>" && S {
B = index($0, ">")
T = substr($0, B+1)
E = index(T, "<")
V = substr(T, 1, E-1)
}
$0 ~ "<fifthtag>" {
B = index($0, ">")
F = substr($0, 1, B)
T = substr($0, B+1)
E = index(T, "<")
R = substr(T, E)
printf "%s%s%s\n", F,V,R
next
}
{
print
}