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:
.........
<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.!!
pamu
February 11, 2013, 6:32am
2
Try
awk -F "[<>]" '/listen-address/ && $3{print $3;exit}' file
First Occurance :
sed -n -e 's/.*<listen-address>\(.*\)<\/listen-address>.*/\1/p' "Your_file.xml" | head -1
Nth Occurance :
sed -n -e 's/.*<listen-address>\(.*\)<\/listen-address>.*/\1/p' "Your_file.xml" | head -n | tail -1
RudiC
February 12, 2013, 4:37am
4
Try this to print the listen-address in exactly the first <server>...</server> tag, or the hostname if missing/empty:
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
1 Like
rudic:
Try this to print the listen-address in exactly the first <server>...</server> tag, or the hostname if missing/empty:
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,
I would like read the result in a variable (myhost) and
for another tag <listen-port> if missing in the first <server> tag then would like to assign "7001" to "myport" variable.
panyam
February 15, 2013, 6:39am
6
Something like this:
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 ?
panyam
February 15, 2013, 7:14am
8
Yes, it will populate empty..!
If you wish to have a default host name, assign it in BEGIN section of my code
something like this:
BEGIN { myh="samplehost";myp="7001" }
panyam:
Yes, it will populate empty..!
If you wish to have a default host name, assign it in BEGIN section of my code
something like this:
BEGIN { myh="samplehost";myp="7001" }
Will this work ? Sorry for being Naive....
myh=$hostname
awk -F"[<->]" 'BEGIN { myh;myp="7001" }
panyam
February 15, 2013, 10:15am
10
no,
Something like this:
awk -F"[<->]" -v myh=$hostname 'BEGIN { myp="7001" }
1 Like