简体   繁体   English

如何(e)grep XML某些标签内容?

[英]How to (e) grep XML for certain tag content?

How can I (e)grep all content between a certain tag block? 我怎样才能(e)grep某个标签块之间的所有内容?

Assume input file below, I want to get as output all characters between the B-tags so: 假设下面的输入文件,我想得到B标签之间所有字符的输出,所以:

<B><C>Test</C></B>
<B>Test2</B>

I tried the following grep to search all XML files with the content between the <B> and </B> tags. 我尝试使用以下grep来搜索所有带有<B></B>标记之间内容的XML文件。

grep '<B>.*</B>' *.xml

but it did not work. 但它不起作用。

For the following input: 对于以下输入:

<A>
 <B>
  <C>Test</C>
 </B>
 <D>
 </D>
 <B>
    Test2
 </B>
</A>

Any ideas? 有任何想法吗?

使用awk:

awk '/<B>/,/<\/B>/'

When working with xml files, the best way is to use xml tools. 使用xml文件时,最好的方法是使用xml工具。

XMLStarlet : XMLStarlet

xmlstarlet sel -t -c '//B' file.xml

xmllint from libxml2 : 来自libxml2的 xmllint:

xmllint --xpath '//B' file.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM