问题
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