|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | 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 and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|