Ashwin
Ashwin

Reputation: 1013

Shell: Extract specific blocks from an XML file

I have a xml file with a same tag occuring multiple times continuously. I need to extract only that set of tags as an array.

xml file looks like:

...
<otherTags>
<myTag>
  <abc/>
</myTag>
<myTag>
  <abc/>
</myTag>
<myTag>
  <abc/>
</myTag>
<otherTags>
...

My output should look like:

<myTag>
  <abc/>
</myTag>

<myTag>
  <abc/>
</myTag>

<myTag>
  <abc/>
</myTag>

Upvotes: 0

Views: 1815

Answers (1)

Jens
Jens

Reputation: 72747

If the format is that simple, use

$ awk '/<myTag>/,/<\/myTag>/' inputfile
<myTag>
  <abc/>
</myTag>
<myTag>
  <abc/>
</myTag>
<myTag>
  <abc/>
</myTag>

But there are tools designed for parsing complex XML files, which you should use if it gets more complicated. One is called xml-grep. You might also want to familiarize yourself with XPath technology.

Upvotes: 1

Related Questions