T A
T A

Reputation: 1756

Shell grep XML content based on tag

I have a xml file from which I want to extract the content inside of two xml tags similar to this:

<foo id="001">
... any String ...
</foo>

The starting tag line is unique due to the identifier, but the closing tag can appear any number of times. Is there a way to output the String inside those two tags using grep?

Upvotes: 0

Views: 680

Answers (1)

user9706
user9706

Reputation:

Try this magic:

grep -oPz '(?s)<foo id="001".*?</foo>\n' file.xml

where:

  • -o Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
  • -P Interpret PATTERNS as Perl-compatible regular expressions (PCREs).
  • -z Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.
  • (?s) Enabled PCRE_DOTALL
  • .*? Makes the match non-greedy (minimal).

Upvotes: 2

Related Questions