Link to home
Start Free TrialLog in
Avatar of jrram
jrramFlag for United States of America

asked on

How can I read data between specific XML tag in Linux (awk, grep, sed, other?)

I have a XML file with the following contents:


In Linux, what's the quickest, cleanest, and most efficient way to read the data between the <Partitions> tag?
Avatar of favoretti
Flag of Netherlands image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ghostdog74

GNU awk

awk 'BEGIN{FS="(<|</)Partitions>"}
/<Partitions>/ {print $2}
'  xmlfile

another one

awk '/Partitions/{gsub(/<|>|\/|Partitions/,"");print} ' xmlfile
Avatar of jrram


Hey favoretti,

I know I've already accepted your solution, but can you update it to support and underscore in the field name?

For example, it doesn't work if it was:

Avatar of jrram


I got it.  Had to ad the underscore to the regular expression:

cat aaa.xml | grep "Partitions" | sed 's/<[A-Za-z\/_]*>//g'
Uh, yeah, that's right. Sorry, I was away for the weekend, didn't see your comment.