任何人都可以帮我编写shell脚本以从下面的xml中找到值:-
<?xml version="1.0" encoding="UTF-8"?>
<log>
<logentry revision="36">
<author>XYZ</author>
<date>2014-07-15T14:47:18.328479Z</date>
<paths>
<path action="A" localPath="ABC" kind="unknown">/branches/ABC</path>
<path action="A" localPath="ABC" kind="unknown">/branches/ABC</path>
<path action="M" localPath="DEF" kind="unknown">/branches/CDF</path>
<path action="M" localPath="DEF" kind="unknown">FRG</path>
</paths>
<msg>added</msg>
</logentry>
</log>
我想提取本地路径的值,即"ABC"和"DEF"作为输出并写入一个文件。
使用 XML 感知工具。例如:
$ xmllint --xpath '/log/logentry/paths/path/@localPath' file.xml
输出:
localPath="ABC" localPath="ABC" localPath="DEF" localPath="DEF"
您可以进一步处理
$ xmllint --xpath '/log/logentry/paths/path/@localPath' file.xml
| sed 's/"/n/g;'
| grep -v '^ '
| sort -u
要得到
ABC
DEF
或使用 XSH
open file.xml ;
$h := hash @localPath /log/logentry/paths/path ;
for { keys %$h } { echo (.) ; }
输出:
ABC
DEF
...或者,使用 XMLStarlet:
xmlstarlet sel -t -v /log/logentry/paths/path/@localPath -n
。发射:
ABC
ABC
DEF
DEF