awk:在XML中搜索关键字并写入另一个文件

时间:2016-07-05 07:38:11

标签: xml bash shell search awk

我的输入XML如下。我需要在输入XML中搜索&#34; SEARCH&#34;关键字存在。如果有,我需要 将内容从<record>复制到</record>并写入另一个XML文件。

输入XML

<XML>
<record category="xyz">
<person ssn="" e-i="E">
<title xsi:nil="true"/>
<position xsi:nil="true"/>
<details>
<names>
<first_name/>
<last_name></last_name>
</names>
<aliases>
<alias>CDP</alias>
</aliases>
<keywords>
<keyword xsi:nil="true"/>
<keyword>SEARCH</keyword>
</keywords>
<external_sources>
<uri>http://www.google.com</uri>
<detail>SEARCH is present in abc for xyz reason</detail>
</external_sources>
</details>
</person>
</record>
<record category="abc">
<person ssn="" e-i="F">
<title xsi:nil="true"/>
<position xsi:nil="true"/>
<details>
<names>
<first_name/>
<last_name></last_name>
</names>
<aliases>
<alias>CDP</alias>
</aliases>
<keywords>
<keyword xsi:nil="true"/>
<keyword>DONTSEARCH</keyword>
</keywords>
<external_sources>
<uri>http://www.google.com</uri>
<detail>SEARCH is not present in abc for xyz reason</detail>
</external_sources>
</details>
</person>
</record>
</XML>

我现在的代码:

NR==FNR {
keywordArray[NR]=$0;
next;
}

/<record / { i=1 }
i { a[i++]=$0 }
/<\/record>/ {
    if (found) {
        for (i=1; i<=length(a); ++i) print a[i] >> output.xml
    }
    i=0;
    found=0
}
$0 ~ "<keyword>"SEARCH"</keyword>" { found=1 }

当前代码问题:

代码没有搜索&#34; SEARCH&#34;并且它没有向output.xml写入任何内容

预期输出:

<record category="xyz">
<person ssn="" e-i="E">
<title xsi:nil="true"/>
<position xsi:nil="true"/>
<details>
<names>
<first_name/>
<last_name></last_name>
</names>
<aliases>
<alias>CDP</alias>
</aliases>
<keywords>
<keyword xsi:nil="true"/>
<keyword>SEARCH</keyword>
</keywords>
<external_sources>
<uri>http://www.google.com</uri>
<detail>SEARCH is present in abc for xyz reason</detail>
</external_sources>
</details>
</person>
</record>

2 个答案:

答案 0 :(得分:1)

嗯,这不完美,但也许你可以改善这个:

BEGIN {
  FS="\n"        # field separator to enter
  OFS="\n"       # output separator as well
  RS="</record>" # records end at </record>
} 
$0 ~ /<keyword>SEARCH<\/keyword>/'     # print record if SEARCH matched

答案 1 :(得分:1)

使用xmlstarlet,您可以使用此功能:

 xmlstarlet sel -t -c "//record[.//keyword/text()='SEARCH']" foo.xml