![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to parse through a file and based on condition form another output file | sivasu.india | UNIX for Advanced & Expert Users | 6 | 02-28-2008 12:59 AM |
| Need help to parse the file | navsharan | Shell Programming and Scripting | 3 | 01-17-2008 11:58 AM |
| Parse file | sbasetty | Shell Programming and Scripting | 5 | 03-27-2007 10:27 AM |
| How to parse a XML file using PERL and XML::DOm | girigopal | Shell Programming and Scripting | 0 | 06-27-2005 03:46 AM |
| using getopt to parse a file | coolguyshail | Shell Programming and Scripting | 1 | 06-08-2005 03:58 AM |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Parse XML file
Hi,
I need to parse the following XML data enclosed in <a> </a> XML tag using shell script. <X> ..... </X> <a> <b> <c>data1</c> <c>data2</c> </b> <d> <c>data3</c> </d> </a> <XX> ... </XX> Further I need to display the data in the following format b data1 data2 ----- d data3 Could any body suggest a way to extract the data residing <a> </a> XML tags. TIA, Viki |
| Forum Sponsor | ||
|
|
|
|||
|
Hi anbu23,
Thanks for quick reply > sed -n "/<a>/,/<\/a>/{/<\/*a>/d;s/^<\([^>]*\)>\([^<]*\)<\/\1>/\2/;s/^<\/.*$/--------------/;s/<\(.*\)>/\1/;p;}" c.xml b c>data1</c c>data2</c /b d c>data3</c /d where c.xml contain the following data. > cat c.xml <X> ..... </X> <a> <b> <c>data1</c> <c>data2</c> </b> <d> <c>data3</c> </d> </a> <XX> ... </XX> The issue is to extract the XML tags i.e. "b" and "d" and then read the XML tag <c>. Further store the data in a text file in the following format b:data1 data2 d:data3 Could you please help me out. TIA, Viki |
|
|||
|
Code:
$ cat file
<X>
.....
</X>
<a>
<b>
<c>data1</c>
<c>data2</c>
</b>
<d>
<c>data3</c>
</d>
</a>
<XX>
...
</XX>
$ sed -n "/<a>/,/<\/a>/{/<\/*a>/d;s/^<\([^>]*\)>\([^<]*\)<\/\1>/\2/;s/^<\/.*$/--------------/;s/<\(.*\)>/\1/;p;}" file
b
data1
data2
--------------
d
data3
--------------
Code:
$ awk -F"[<>]" ' /<a>/,/<\/a>/ {
> if ( $0 !~ /<\/*a>/ ) {
> if ( $0 == "</" tag ">" ) { print str }
> else if ( NF == 3 ) { str = $2 ":" ; tag=$2 }
> else { str = str " " $3 }
> }
> } ' file
b: data1 data2
d: data3
|
| Thread Tools | |
| Display Modes | |
|
|