AWK:解析并写入到另一个文件 [英] awk : parse and write to another file
本文介绍了AWK:解析并写入到另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在XML文件中的记录如下图所示。我需要搜索<&关键字GT;搜索和LT; /关键字>
如果present
然后,我需要整个记录,并写入到另一个文件(从&LT开始。记录>
到< /记录>
)
下面是我的awk code这是内部循环。 $ 1
按每条记录的行值保持一致。
如果(指数($ 1,搜索)大于0)
{
打印$ 1 GT;> output.txt的
}
这逻辑有两个问题,
- 这是写入
output.txt的
文件,只有<关键字>搜索和LT; /关键字>
元素而不是整个记录(启动<记录>
到< /记录>
) - 搜索,也可以在
&LT present;详情>
标记。这code甚至会写标记output.txt的
XML文件:
<记录类=XYZ>
<人SSN =E-I =E>
<标题XSI:零=真/>
<仓位XSI:零=真/>
<姓名和GT;
< FIRST_NAME />
<&姓氏GT;< /姓氏>
<&别名GT;
<&别名GT; CDP< /别名>
< /别名>
<&关键词GT;
<关键字XSI:零=真/>
<&关键字GT;搜索和LT; /关键字>
< /关键字>
< external_sources>
&LT;&URI GT; HTTP://www.google.com< / URI&GT;
&LT;详情&GT;搜索,ABC为XYZ原因&LT present; /详情&GT;
&LT; / external_sources&GT;
&LT; /细节&GT;
&LT; /记录&GT;
&LT;记录类=ABC&GT;
&LT;人SSN =E-I =F&GT;
&LT;标题XSI:零=真/&GT;
&LT;仓位XSI:零=真/&GT;
&LT;姓名和GT;
&LT; FIRST_NAME /&GT;
&LT;&姓氏GT;&LT; /姓氏&GT;
&LT;&别名GT;
&LT;&别名GT; CDP&LT; /别名&GT;
&LT; /别名&GT;
&LT;&关键词GT;
&LT;关键字XSI:零=真/&GT;
&LT;&关键字GT; DONTSEARCH&LT; /关键字&GT;
&LT; /关键字&GT;
&LT; external_sources&GT;
&LT;&URI GT; HTTP://www.google.com< / URI&GT;
&LT;详情&GT;搜索是不是在ABC的XYZ原因&LT present; /详情&GT;
&LT; / external_sources&GT;
&LT; /细节&GT;
&LT; /记录&GT;
解决方案
使用GNU AWK为多字符RS:
$ AWK -v RS ='&LT; /记录&GT; \\ N''{ORS = RT} /&LT;关键字&GT;搜索和LT; \\ /关键字&GT; /文件
&LT;记录类=XYZ&GT;
&LT;人SSN =E-I =E&GT;
&LT;标题XSI:零=真/&GT;
&LT;仓位XSI:零=真/&GT;
&LT;姓名和GT;
&LT; FIRST_NAME /&GT;
&LT;&姓氏GT;&LT; /姓氏&GT;
&LT;&别名GT;
&LT;&别名GT; CDP&LT; /别名&GT;
&LT; /别名&GT;
&LT;&关键词GT;
&LT;关键字XSI:零=真/&GT;
&LT;&关键字GT;搜索和LT; /关键字&GT;
&LT; /关键字&GT;
&LT; external_sources&GT;
&LT;&URI GT; HTTP://www.google.com< / URI&GT;
&LT;详情&GT;搜索,ABC为XYZ原因&LT present; /详情&GT;
&LT; / external_sources&GT;
&LT; /细节&GT;
&LT; /记录&GT;
如果你需要搜索的多个关键字,那么简单地列出它们的方式:
$ AWK -v RS ='&LT; /记录&GT; \\ N''{ORS = RT} /&LT;关键字&GT;(搜索1 |搜索2 | SEARCH3)LT; \\ /关键字&GT ; /文件
I have records in XML file like below. I need to search for <keyword>SEARCH</keyword>
and if present
then I need to take the entire record and write to another file.(starting from <record>
to </record>
)
Below is my awk code which is inside loop. $1
holds line by line value of each record.
if(index($1,"SEARCH")>0)
{
print $1>> "output.txt"
}
This logic has two problems,
- It is writing to
output.txt
file, only<keyword>SEARCH</keyword>
element and not the whole record(starting from<record>
to</record>
) - SEARCH can also be present in
<detail>
tag. This code will even write that tag tooutput.txt
XML File:
<record category="xyz">
<person ssn="" e-i="E">
<title xsi:nil="true"/>
<position xsi:nil="true"/>
<names>
<first_name/>
<last_name></last_name>
<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>
</record>
<record category="abc">
<person ssn="" e-i="F">
<title xsi:nil="true"/>
<position xsi:nil="true"/>
<names>
<first_name/>
<last_name></last_name>
<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>
</record>
解决方案
Use GNU awk for multi-char RS:
$ awk -v RS='</record>\n' '{ORS=RT} /<keyword>SEARCH<\/keyword>/' file
<record category="xyz">
<person ssn="" e-i="E">
<title xsi:nil="true"/>
<position xsi:nil="true"/>
<names>
<first_name/>
<last_name></last_name>
<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>
</record>
If you need to search for any of multiple keywords then simply list them as such:
$ awk -v RS='</record>\n' '{ORS=RT} /<keyword>(SEARCH1|SEARCH2|SEARCH3)<\/keyword>/' file
这篇关于AWK:解析并写入到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文