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

蹲街弑〆低调 提交于 2020-01-15 10:05:26

问题


How can I (e)grep all content between a certain tag block?

Assume input file below, I want to get as output all characters between the B-tags so:

<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

but it did not work.

For the following input:

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

Any ideas?


回答1:


Use awk:

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



回答2:


When working with xml files, the best way is to use xml tools.

XMLStarlet:

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

xmllint from libxml2:

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


来源:https://stackoverflow.com/questions/10714716/how-to-e-grep-xml-for-certain-tag-content

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!