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:

   <Misc>
      <FetchSizeHint>32000</FetchSizeHint>
      <Partitions>100</Partitions>
      <ThreadCount>1</ThreadCount>
      <NullConversion>true</NullConversion>
      <RollingDays>30</RollingDays>
      <BytesPerChar>1</BytesPerChar>
   </Misc>

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

Link to home
membership
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
ghostdog74

GNU awk

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


another one

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

ASKER

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:

<Partitions_X>100</Partitions_X>
Avatar of jrram

ASKER

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.