sed parse small xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed parse small xml file
# 1  
Old 04-19-2010
sed parse small xml file

I have a tmp.xml file like:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Ip>193.143.121.198</Ip>
  <Status>OK</Status>
  <CountryCode>PL</CountryCode>
  <CountryName>Poland</CountryName>
  <RegionCode>82</RegionCode>
  <RegionName>Pomorskie</RegionName>
  <City>Gdansk</City>
  <ZipPostalCode></ZipPostalCode>
  <Latitude>54.35</Latitude>
  <Longitude>18.6667</Longitude>
  <Timezone>0</Timezone>
  <Gmtoffset>0</Gmtoffset>
  <Dstoffset>0</Dstoffset>
</Response>

where I'm trying to search for specific fields like this:
Code:
cc=`grep CountryCode tmp.xml | tr -d '\t' | sed 's/^<.*>\([^<].*\)<.*>$/\1/' `
echo $cc
rn=`grep RegionName tmp.xml | tr -d '\t' | sed 's/^<.*>\([^<].*\)<.*>$/\1/' `
echo $rn
ct=`grep City tmp.xml | tr -d '\t' | sed 's/^<.*>\([^<].*\)<.*>$/\1/' `
echo $ct

which outputs
Code:
<CountryCode>PL</CountryCode>
<CountryName>Poland</CountryName>
<RegionName>Pomorskie</RegionName>
<City>Gdansk</City>

I want an ouput like:
Code:
PL
Poland
Pomorskie
Gdansk

what is wrong with my sed command and is there a better/more elegant way to do this?
# 2  
Old 04-19-2010
Try:
Code:
awk -F"[<>]" '/CountryCode/||/CountryName/||/RegionName/||/City/{print $3}' file

# 3  
Old 04-19-2010
Shorter Smilie
Code:
awk -F"[<>]" '/Country|RegionName|City/{print $3}' file

# 4  
Old 04-19-2010
A sed version...

Code:
sed -n 's/^.*<\(CountryCode\|CountryName\|RegionName\|City\)>\([^<]*\)<\/.*$/\2/p

js.
# 5  
Old 04-19-2010
MySQL

you can try this Smilie

Code:
[root@rhnserver ~]# grep CountryCode xml | tr -d '\t' |  sed 's/\(.*\)>\(.*\)<\(.*\)/\2/'
PL

# 6  
Old 04-19-2010
Code:
#!/bin/sh

[ "$1" ] && exec < "$1"

IFS='<>'
while read -r __ key value __; do
    case $key in 
        CountryCode|CountryName|RegionName|City) echo $value;;
    esac
done

If optional file argument is not provided, read from standard input.

Regards,
Alister
# 7  
Old 04-19-2010
This one is most elegant, imho, hehe:
Code:
#!/bin/perl

use XML::Simple;

$xml = new XML::Simple;

$data = $xml->XMLin("tmp.xml");

print "$data->{CountryCode}\n$data->{CountryName}\n$data->{RegionName}\n$data->{City}\n";

Enjoy!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parse xml file

I am trying to create a shell script that will parse an xml file (file attached). awk '/Id v=/ { print }' Test.xml | sed 's!<Id v=\"\(.*\)\"/>!\1!' > output.txt An output.txt file is created but it is empty. It should contain the value 222159 in it. Thanks. (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Parse XML File.

HI Guys I have Below XML File : <xn:SubNetwork id="XYZ"> <xn:SubNetwork id="C01"> <xn:MeContext id="CO1"> <xn:ManagedElement id="1"> <un:RncFunction id="1"> <un:UtranCell id="NY431"> ... (2 Replies)
Discussion started by: pareshkp
2 Replies

3. UNIX for Dummies Questions & Answers

Parse xml file

HI Guys, Input .XML <xn:MeContext id="L0307"> <xn:ManagedElement id="1"> <xn:VsDataContainer id="1"> <xn:attributes> <xn:vsDataType>vsDataENodeBFunction</xn:vsDataType> ... (3 Replies)
Discussion started by: pareshkp
3 Replies

4. Programming

Parse XML file

How do I get the field info for tags ID, NAME, DESCRIPTION. Below is my current code put I can't get beyond the first_child of the file. use strict; use warnings; use XML::Simplehttp://images.intellitxt.com/ast/adTypes/icon1.png; use... (1 Reply)
Discussion started by: leemalloy
1 Replies

5. Shell Programming and Scripting

parse xml file

Hello all, Given the following extract from a xml file with multiple <JOB> .... </JOB> entries <JOB APPLICATION="APP" APR="0" AUG="0" AUTHOR="AUT" AUTOARCH="0" CMDLINE="/tmp/test1 %%var" CONFIRM="1" CREATION_DATE="20100430" CREATION_TIME="130739" ... (2 Replies)
Discussion started by: cabrao
2 Replies

6. Shell Programming and Scripting

xmlstarlet template parse small xml file

I have a file like: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <geonames> <geoname> <toponymName>Palos Verdes</toponymName> <name>Palos Verdes</name> <lat>42.1628912</lat> <lng>-123.6481235</lng> <geonameId>5718340</geonameId> <countryCode>US</countryCode>... (4 Replies)
Discussion started by: unclecameron
4 Replies

7. Emergency UNIX and Linux Support

How to parse the following xml file

Hi, I have the following file Example.xml <?xml version="1.0" encoding="iso-8859-1"?> <html><set label="09/07/29" value="1241.90"/> </html> Can any one help me in parsing this xml file I want to retrive the attribute values of the tag set Example I want to... (3 Replies)
Discussion started by: Raji_gadam
3 Replies

8. Shell Programming and Scripting

How can I parse xml file?

How can I parse file containing xml ? I am sure that its best to use perl - but my perl is not very good - can someone help? Example below contents of file containing the xml - I basically want to parse the file and have each field contained in a variable.. ie. I want to store the account... (14 Replies)
Discussion started by: frustrated1
14 Replies

9. Shell Programming and Scripting

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> (5 Replies)
Discussion started by: viki
5 Replies

10. 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
Login or Register to Ask a Question