Visit Our UNIX and Linux User Community


how to parse the file in xml format using awk/nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to parse the file in xml format using awk/nawk
# 1  
Old 09-10-2009
how to parse the file in xml format using awk/nawk

Hi All,
I have an xml file with the below format.
Code:
<a>111</a><b>222</b><c>333<c><d><e>123</e><f>234</f><d><e>456</e><f>789</f>

output needed is
Code:
111,222,333,123,234
111,222,333,456,789

Code:
nawk 'BEGIN{FS="<|>"}
        
       {print a,b,c,e,f
                   a=""
                   b=""
                   e=""
                   f=""
          }         
        
         {for(i=1;i<=NF;i++) {if($i=="a"){a=$(i+1);continue}}}
         {for(i=1;i<=NF;i++) {if($i=="b"){b=$(i+1); continue}}}
         {for(i=1;i<=NF;i++) {if($i=="c"){d=$(i+1); continue}}}
         {for(i=1;i<=NF;i++) {if($i=="e"){d=$(i+1); continue}}}
         {for(i=1;i<=NF;i++) {if($i=="f"){d=$(i+1); continue}}}
       END {print a,b,c,e,f}' file

However,
the output that I have is
Code:
111,222,333,456,789

ANy one have any idea?

Last edited by Franklin52; 09-11-2009 at 03:15 AM.. Reason: Please use code tags!
# 2  
Old 09-11-2009
lots of threads are available regarding this.
please use search.
# 3  
Old 09-11-2009
Trick is in using the right tool for the right job.

There are modules already available in CPAN for xml parsing and creating xml stuff. Try them instead! Smilie
# 4  
Old 09-11-2009
Quote:
there are modules already available in CPAN for xml parsing and creating xml stuff. Try them instead!
Unfortunately, if you look closely at the string, it is not valid XML as it is not well-formed. No XML parser is going to handle this string.

---------- Post updated at 10:21 AM ---------- Previous update was at 09:06 AM ----------

One way of doing it would be to use a mix of sed and awk to parse and process the line
Code:
sed 's/\<d\>/|/g' file | sed 's/\<\/.\>/ /g' | sed 's/\<.\>//g' | sed 's/ \(.\)/,\1/g' | \
sed 's/,|/|/g' |  awk -F'|' '{ printf "%s,%s\n", $1, $2; printf "%s,%s\n", $1, $3 }'

This outputs
Code:
111,222,333,123,234
111,222,333,456,789

Not elegant but it works!
# 5  
Old 09-14-2009
Code:
$_='<a>111</a><b>222</b><c>333<c><d><e>123</e><f>234</f><d><e>456</e><f>789</f>';
my @tmp=$_=~/[0-9]+/g;
my @a1=@tmp[0..4];
my @a2=@tmp[0..2,5,6];
print join ",", @a1;
print "\n";
print join ",",@a2;

# 6  
Old 09-14-2009
Hi anchal _khare,matrixmadhan,fpmurphy,summer_cherry

Thank you very much for your help!!

---------- Post updated at 05:01 AM ---------- Previous update was at 05:00 AM ----------

Quote:
Originally Posted by summer_cherry
Code:
$_='<a>111</a><b>222</b><c>333<c><d><e>123</e><f>234</f><d><e>456</e><f>789</f>';
my @tmp=$_=~/[0-9]+/g;
my @a1=@tmp[0..4];
my @a2=@tmp[0..2,5,6];
print join ",", @a1;
print "\n";
print join ",",@a2;


Hi summer_cherry,

This is the perl script?

Thanks.
# 7  
Old 09-14-2009
Another way with awk...
Code:
 
awk -F"<d>" '{print $1","$2,"\n"$1","$3}' f1 | tr -d '<[a-z]>' | tr '\/' ','


Previous Thread | Next Thread
Test Your Knowledge in Computers #274
Difficulty: Easy
OpenSolaris is a descendant of the UNIX System V Release 4 (SVR4) code base developed by Sun and AT&T in the late 1980s.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Date format conversion how to change this from using nawk to awk

Hi, I have a file where I need to change the date format on the nth field from DD-MM-YYYY to YYYY-MM-DD so I can accurately sort the record by dates From regex - Use sed or awk to fix date format - Stack Overflow, I found an example using nawk. Test run as below: $: cat xyz.txt A ... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

How to parse this file using awk and output in CSV format?

My source file looks like this: Cust-Number = "101" Cust-Name="Joe" Cust-Town="London" Cust-hobby="tennis" Cust-purchase="200" Cust-Number = "102" Cust-Name="Mary" Cust-Town="Newyork" Cust-hobby="reading" Cust-purchase="125" Now I want to parse this file (leaving out hobby) and... (10 Replies)
Discussion started by: Balav
10 Replies

3. Shell Programming and Scripting

AWK to Parse XML messages

Hello Guys, Please help with AWK problem. I have XML file which contains a list of messages for subjects. Example of the messages: , message=, message=, message=, message=, message=, message=, message=, message= I want to use AWK to parse these xml messages but I am... (7 Replies)
Discussion started by: James_Owen
7 Replies

4. Shell Programming and Scripting

Help nawk parse file

Hello, Hope you are doing fine. I have a file in following format. I only want to process the data inside the section that comes after #DATE,CODE,VALUE #ITEMS WITH CORRECTIONS ....... #DATE,CODE,VALUE 2011-08-02, ID1, 0.30 2011-08-02, ID2, 0.40 2011-08-02, ID3, 0.50 ...... Means... (3 Replies)
Discussion started by: srattani
3 Replies

5. UNIX for Dummies Questions & Answers

Parse XML e report con AWK

Thanks in advance who can answer. I have to make a small shell that after reading an XML file and extract the fields I create a text file with the same fields taken previously in tabular form. I did this parse.awk ---------------------- BEGIN { FS="" } { for(i=2; i<=NF; i+=2) { ... (1 Reply)
Discussion started by: mcarlo65
1 Replies

6. Shell Programming and Scripting

parsing(xml) using nawk/awk

Hi , I have an xml format as shown below: <Info> <last name="sean" first name="john"/> <period="5" time="11"/> <test value="1",test2 value="2",test3 value="3",test4 value="5"> <old> <value1>1</value1> <value2>2</value2> </old> <new> <value1>4</value1> <value2>3</value2> </new>... (1 Reply)
Discussion started by: natalie23
1 Replies

7. Shell Programming and Scripting

awk/nawk question to format a file

Hi, I am new to awk/nawk, needs help. I want to merge the rows having emplid attribute same into a single row in the following file. In actual this kind of file will have around 50k rows. Here is my input file id|emplid|firstname|dep|lastname 1|001234|test|1001|1 2|002345|test|1032|2... (7 Replies)
Discussion started by: kumar04
7 Replies

8. Shell Programming and Scripting

Need AWk To parse XML logs

Hi , I need an Awk script to parse my log file . 2008-04-26 10:00:13,391 INFO Logger - <?xml version="1.0" encoding="UTF-8" standalone="no"?><2dm tmsg... (0 Replies)
Discussion started by: amit1_x
0 Replies

9. Shell Programming and Scripting

How to parse a XML file using PERL and XML::DOm

I need to know the way. I have got parsing down some nodes. But I was unable to get the child node perfectly. If you have code please send it. It will be very useful for me. (0 Replies)
Discussion started by: girigopal
0 Replies

Featured Tech Videos