Data between XML Tags

<?xml version="1.0" encoding="iso-8859-1" ?>
<TABLE>
   <TEST>
      <ID> 123 </ID>
      <name> abc </name>
   </TEST>
   <TEST>
      <ID> 123 </ID>
      <name> abc2 </name>
   </TEST>
</TABLE>
<TABLE>
   <TEST>
      <ID> 456 </ID>
      <name> def </name>
   </TEST>
   <TEST>
      <ID> 456 </ID>
      <name> def2 </name>
   </TEST>
</TABLE>

how to get data between the tags <TABLE> </TABLE> as individual rows in a file?

What would the output you want actually look like?

What would you like your output file to look like and are there things you have been trying yourself?

Okay.. The actual output I need is two columns ID and XML and two rows with ID Values 123, 456 and XML values should be the whole text between the tags table. If this cannot be achieved let me know how to extract data between the tags table and write it to a file. I can work out from there.

ID|XML
123|<TABLE> <TEST> <ID> 123 </ID> <name> abc </name> </TEST> <TEST> <ID> 123 </ID> <name> abc2 </name> </TEST> </TABLE>
456|<TABLE> <TEST> <ID> 456 </ID> <name> def </name> </TEST> <TEST> <ID> 456 </ID> <name> def2 </name> </TEST> </TABLE>

Alright, what have you tried so far, eskay?

I haven't tried anything worthy. I am trying few sed and awk commands which I found here like sed -n '/<TABLE>/,/<\/TABLE>/p' test.xml but I am getting the whole data. I am newbie to UNIX shell scripting.

OK, see if this works:

awk '/<TABLE/{print RS}NR>1' infile | awk 'BEGIN{print "ID|XML"} {$1=$4"|"$1}1' RS=

A prerequisite is that the id tags are always in the right order within that table, otherwise you need to use something a little bit more complicated..

1 Like

Oh my god! I ran it with sample file and it worked. I will come back if I have any problem while doing with the big main file.

You are one hell of a genius man! wow... how can i get to learn the stuff? simply wow!

Thanks, I hope it helps to find a solution.

I like this O'Reilly book:
sed & awk, 2nd Edition-O'Reilly Media

There is also a solid on-line tutorial:
Awk - A Tutorial and Introduction - by Bruce Barnett

Other ingredients are practice and hanging out on these forums :wink: