Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Extract string from XML

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-11-2013
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 12 July 2015, 8:02 AM EDT
Posts: 403
Thanks: 93
Thanked 0 Times in 0 Posts
Scissors Extract string from XML

Hi,

I wish to grep for the first instance of <listen-address> value between the first <server></server> tag in an xml file.

Sample xml:




Code:
.........
    <timeout-seconds>1500</timeout-seconds>
  </jta>
  <server>
    <name>Adminserver_DEV</name>
    <listen-address>myadminhost</listen-address>
  </server>
  <server>
    <name>managed_core</name>
<listen-address>secondadminhost</listen-address>
    <ssl>
      <enabled>false</enabled>
......

Desired Result: myadminhost

If<listen-address></listen-address> is blank or not present in the first <server></server> tag then the desired result should be the hostname of the server excuting the script.

Thank you.!!
Sponsored Links
    #2  
Old Unix and Linux 02-11-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 9 July 2015, 10:56 AM EDT
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts
Try


Code:
 
awk -F "[<>]" '/listen-address/ && $3{print $3;exit}' file

Sponsored Links
    #3  
Old Unix and Linux 02-11-2013
Shaishav Shah Shaishav Shah is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 21 January 2014, 6:53 AM EST
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
First Occurance :


Code:
sed -n -e 's/.*<listen-address>\(.*\)<\/listen-address>.*/\1/p' "Your_file.xml" | head -1

Nth Occurance :


Code:
sed -n -e 's/.*<listen-address>\(.*\)<\/listen-address>.*/\1/p' "Your_file.xml" | head -n | tail -1

    #4  
Old Unix and Linux 02-12-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 27 July 2015, 4:19 PM EDT
Location: Aachen, Germany
Posts: 6,377
Thanks: 112
Thanked 1,760 Times in 1,646 Posts
Try this to print the listen-address in exactly the first <server>...</server> tag, or the hostname if missing/empty:
Code:
awk     'BEGIN {"hostname" | getline HN}
         /<\/server>/ {exit}
         /<server>/   {s=1}
         /<listen-address>/&& s {gsub (/<.?listen-address>| */, "")
                                 LA = $0
                                }
         END            {print LA?LA:HN
                        }
        ' file
myadminhost

The Following User Says Thank You to RudiC For This Useful Post:
mohtashims (02-15-2013)
Sponsored Links
    #5  
Old Unix and Linux 02-15-2013
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 12 July 2015, 8:02 AM EDT
Posts: 403
Thanks: 93
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by RudiC View Post
Try this to print the listen-address in exactly the first <server>...</server> tag, or the hostname if missing/empty:
Code:
awk     'BEGIN {"hostname" | getline HN}
         /<\/server>/ {exit}
         /<server>/   {s=1}
         /<listen-address>/&& s {gsub (/<.?listen-address>| */, "")
                                 LA = $0
                                }
         END            {print LA?LA:HN
                        }
        ' file
myadminhost

This is more close to my request as it considers the first <server> tag only. However,

1. I would like read the result in a variable (myhost) and

2. for another tag <listen-port> if missing in the first <server> tag then would like to assign "7001" to "myport" variable.
Sponsored Links
    #6  
Old Unix and Linux 02-15-2013
panyam panyam is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 8 December 2014, 6:40 AM EST
Posts: 1,159
Thanks: 20
Thanked 105 Times in 100 Posts
Something like this:


Code:
 awk -F"[<->]" ' BEGIN { myh="";myp="7001" }
 /<\/server>/ { exit } 
 /<server>/ { s = 1 ; }
 s && /<listen-address>/ {  myh=$3; }  
 /<listen-port>/ {  myp=$3 ;}
 END { print "host:"myh"\t port->"myp }' sample

Sponsored Links
    #7  
Old Unix and Linux 02-15-2013
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 12 July 2015, 8:02 AM EDT
Posts: 403
Thanks: 93
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by panyam View Post
Something like this:


Code:
 awk -F"[<->]" ' BEGIN { myh="";myp="7001" }
 /<\/server>/ { exit } 
 /<server>/ { s = 1 ; }
 s && /<listen-address>/ {  myh=$3; }  
 /<listen-port>/ {  myp=$3 ;}
 END { print "host:"myh"\t port->"myp }' sample

But will this populate the hostname command if <listen-address> tag is not found ?
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
XML - Split And Extract String between Chars unme Shell Programming and Scripting 2 12-19-2012 04:43 AM
Extract Multivalue from XML manas_ranjan Shell Programming and Scripting 4 01-09-2012 06:16 AM
Extract value from XML manas_ranjan Shell Programming and Scripting 9 01-08-2012 01:23 AM
to extract string from main string and string comparison vivek d r Shell Programming and Scripting 2 11-16-2011 07:17 AM
Grep a string from input file and delete next three lines including the line contains string in xml greet_sed Shell Programming and Scripting 4 09-28-2011 05:18 AM



All times are GMT -4. The time now is 01:48 AM.