awk Script to parse a XML tag | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

awk Script to parse a XML tag

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-19-2012
asirohi asirohi is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 19 November 2012, 11:35 PM EST
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
awk Script to parse a XML tag

I have an XML tag like this:
<property name="agent" value="/var/tmp/root/eclipse" />

Is there way using awk that i can get the value from the above tag. So the output should be:
/var/tmp/root/eclipse

Help will be appreciated.

Regards,
Adi
Sponsored Links
    #2  
Old 11-19-2012
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Last Activity: 7 April 2014, 3:02 PM EDT
Location: earth>US>UTC-5
Posts: 1,466
Thanks: 110
Thanked 506 Times in 485 Posts
If you're using a gnu-like awk that supports a record separator pattern, this might work for you:


Code:
awk '
    /property name=/ {
        gsub( ".*value=\"", "" );
        gsub( "\".*", "" );
        print;
    }
' RS="[<>]"  input-file

Sponsored Links
    #3  
Old 11-19-2012
asirohi asirohi is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 19 November 2012, 11:35 PM EST
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Agama,

Thanks for the reply, but that does not work. The script that you provided just removes the <> from the line and displays
property name="agent" value="/var/tmp/root/eclipse" /
as output.

-Adi
    #4  
Old 11-19-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 29 August 2014, 5:55 PM EDT
Location: Galactic Empire
Posts: 3,362
Thanks: 230
Thanked 1,197 Times in 1,125 Posts
If that xml tag does not have a different attribute, you can simply do:-

Code:
awk -F\" '/property name=/ { print $(NF-1); } ' xml_file

Sponsored Links
    #5  
Old 11-19-2012
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Last Activity: 7 April 2014, 3:02 PM EDT
Location: earth>US>UTC-5
Posts: 1,466
Thanks: 110
Thanked 506 Times in 485 Posts
Quote:
Originally Posted by asirohi View Post
Hi Agama,

Thanks for the reply, but that does not work. The script that you provided just removes the <> from the line and displays
property name="agent" value="/var/tmp/root/eclipse" /
as output.

-Adi
How were you testing it? if you were using echo to echo it and pipe it into awk, were you using double quotes round the whole string? That won't work. Use single quotes:


Code:
echo '<property name="agent" value="/var/tmp/root/eclipse" />' | awk '
    /property name=/ {
        gsub( ".*value=\"", "" );
        gsub( "\".*", "" );
        print;
    }
' RS="[<>]"

If you're testing some other way, then I'd be curious what your version of awk is. Works for me with gnu awk 4.0; output from above is

Code:
/var/tmp/root/eclipse

Sponsored Links
    #6  
Old 11-19-2012
asirohi asirohi is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 19 November 2012, 11:35 PM EST
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Agama,

You solution works but i just confirmed the XML tag is:
<property name='agent' value='/var/tmp/root/eclipse' />

instead of a ", its ' to represent String attribute.
How would i modify your script now?

-Adi
Sponsored Links
    #7  
Old 11-19-2012
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Last Activity: 7 April 2014, 3:02 PM EDT
Location: earth>US>UTC-5
Posts: 1,466
Thanks: 110
Thanked 506 Times in 485 Posts
Ah, very good, thanks.

Have a go with this:

Code:
 awk '
    /property name=/ {
        gsub( ".*value=" Q, "" );
        gsub( Q ".*", "" );
        print;
    }
' Q="'" RS="[<>]"

Embedding single quotes inside of an awk programme contained inside of single quotes is tricky. Several ways of dealing with it; I think this is the easiest. It assigns the single quote to Q, and then appends it to the strings in the substitution commands where needed.
The Following User Says Thank You to agama For This Useful Post:
itkamaraj (11-20-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to retrieve the value from XML tag whose end tag is in next line mjavalkar Shell Programming and Scripting 3 05-03-2012 02:12 AM
AWK to Parse XML messages James_Owen Shell Programming and Scripting 7 11-08-2011 05:25 PM
Shell script (not Perl) to parse xml with awk Pluff Shell Programming and Scripting 2 03-03-2011 01:02 PM
Need AWk To parse XML logs amit1_x Shell Programming and Scripting 0 05-08-2008 08:46 AM
Searching XML tag in a script abhandari Shell Programming and Scripting 1 05-25-2006 12:31 AM



All times are GMT -4. The time now is 12:13 AM.