Extract "Name" from this syntax.....

Hello Folks

      <Server ConnectTimeout="0" ExtendedHandshake="false" MaxConnections="-1" Name="JVM1" ServerIOTimeout="0" WaitForContinue="false">
         <Transport Hostname="<HOST_NAME>" Port="9365" Protocol="http"/>
         <Transport Hostname="<HOST_NAME>" Port="9565" Protocol="https">
            <Property Name="keyring" Value="<WAS_HOME>/plugin-key.kdb"/>
            <Property Name="stashfile" Value="<WAS_HOME>/plugin-key.sth"/>
         </Transport>
      </Server>

If the port number is known,how can i get the "Name=JVM1' from above syntax?

Above lines are a part of an xml file...say 'abc.xml'.

Regards
Abhi

Something like this:

 
awk -v port="9365" '$0 ~ port { print c;next } {c=$0;next }'  inputfile | sed 's/.*\(Name=.*\)S.*/\1/'

Using only awk , assuming that the position on the Name=.. will be same in the all the lines.

awk -v port="9365" '$0 ~ port { print a[5];next } {split($0,a," ");next }' inputfile 

The shell variable p contains the port number (and nothing else):

sed -n '/ Port="'$p'"/{g;p;q;}; /.*<Server .* Name="\([^"]*\).*/{s//\1/;h;}'
grep -B2 "9365" | awk ' /Server/ {print $5}'

Guys

Can you show me how your syntaxes are working on your system ?

'awk' is throwing me syntax error and 'sed' is not returning anything....

Regards
Abhi

Here is the test for alister's sed and my awk :wink:

# p=9565
# sed -n '/ Port="'$p'"/{g;p;q;}; /.*<Server .* Name="\([^"]*\).*/{s//\1/;h;}' file
JVM1
# awk -F\" '/<Server/{n=$8}/Port/&& $4 ~ port{print n,$4}' port=9565 file
JVM1 9565
JVM2 9565
# cat file
      <Server ConnectTimeout="0" ExtendedHandshake="false" MaxConnections="-1" Name="JVM1" ServerIOTimeout="0" WaitForContinue="false">
         <Transport Hostname="<HOST_NAME>" Port="9365" Protocol="http"/>
         <Transport Hostname="<HOST_NAME>" Port="9565" Protocol="https">
            <Property Name="keyring" Value="<WAS_HOME>/plugin-key.kdb"/>
            <Property Name="stashfile" Value="<WAS_HOME>/plugin-key.sth"/>
         </Transport>
      </Server>
      <Server ConnectTimeout="0" ExtendedHandshake="false" MaxConnections="-1" Name="JVM2" ServerIOTimeout="0" WaitForContinue="false">
         <Transport Hostname="<HOST_NAME>" Port="9365" Protocol="http"/>
         <Transport Hostname="<HOST_NAME>" Port="9565" Protocol="https">
            <Property Name="keyring" Value="<WAS_HOME>/plugin-key.kdb"/>
            <Property Name="stashfile" Value="<WAS_HOME>/plugin-key.sth"/>
         </Transport>
      </Server>

If you can not run the code please post you system OS & shell used.

bash-3.00$ cat name.xml
<Server ConnectTimeout="0" ExtendedHandshake="false" MaxConnections="-1" Name="JVM1" ServerIOTimeout="0" WaitForContinue="false">
        <Transport Hostname="<HOST_NAME>" Port="9365" Protocol="http"/>
        <Transport Hostname="<HOST_NAME>" Port="9565" Protocol="https">
                <Property Name="keyring" Value="<WAS_HOME>/plugin-key.kdb"/>
                <Property Name="stashfile" Value="<WAS_HOME>/plugin-key.sth"/>
        </Transport>
</Server>
bash-3.00$ p=9565
bash-3.00$ sed -n '/ Port="'$p'"/{g;p;q;}; /.*<Server .* Name="\([^"]*\).*/{s//\1/;h;}' name.xml

bash-3.00$ awk -F\" '/<Server/{n=$8}/Port/&& $4 ~ port{print n,$4}' port=9565 name.xml
awk: syntax error near line 1
awk: bailing out near line 1
bash-3.00$ awk -F\" '/<Server/{n=$8}/Port/&& $4 ~ port{print n,$4}' Port=9565 name.xml
awk: syntax error near line 1
awk: bailing out near line 1
bash-3.00$ cat name.xml |  awk -F\" '/<Server/{n=$8}/Port/&& $4 ~ port{print n,$4}'
awk: syntax error near line 1
awk: bailing out near line 1

O.S is Solaris 10.

I guess i need to provide actual xml file here.File format might be issue here.

Regards
Abhi

use other versions of awk ,

might be nawk / gawk

Use /usr/xpg4/bin/awk on Solaris.

Can anyone try with this actual xml ?

I would really appreciate your time and effort.

Regards
Abhi

I tried this:

 
awk -v port="9363" '$0 ~ port { print a[6];next } {split($0,a," ");next }' input_file | sed '/^$/d' 
Name="was7node1_userapp1"
 
awk -v port="9365" '$0 ~ port { print a[6];next } {split($0,a," ");next }' input_file | sed '/^$/d'
Name="was7node1_userapp3"

I take a data sample from your file and works as expected.

# cat data.sample
      <PrimaryServers>
         <Server Name="was7NDnode_dmgr"/>
      </PrimaryServers>
   </ServerCluster>
   <ServerCluster CloneSeparatorChange="false" GetDWLMTable="false" IgnoreAffinityRequests="true" LoadBalance="Round Robin" Name="sample_server1_was7node1_Cluster" PostBufferSize="64" PostSizeLimit="-1" RemoveSpecialHeaders="true" RetryInterval="60">
      <Server ConnectTimeout="0" ExtendedHandshake="false" MaxConnections="-1" Name="was7node1_sample_server1" ServerIOTimeout="0" WaitForContinue="false">
         <Transport Hostname="HOST_NAME" Port="9360" Protocol="http"/>
         <Transport Hostname="HOST_NAME" Port="9560" Protocol="https">
            <Property Name="keyring" Value="/global/site/vendor/WAS/WebSphere7/AppServer/Plugin/etc/plugin-key.kdb"/>
            <Property Name="stashfile" Value="/global/site/vendor/WAS/WebSphere7/AppServer/Plugin/etc/plugin-key.sth"/>
         </Transport>
      </Server>
      <PrimaryServers>
         <Server Name="was7node1_sample_server1"/>
      </PrimaryServers>
   </ServerCluster>
   <ServerCluster CloneSeparatorChange="false" GetDWLMTable="false" IgnoreAffinityRequests="true" LoadBalance="Round Robin" Name="TestServer_was7node1_Cluster" PostBufferSize="64" PostSizeLimit="-1" RemoveSpecialHeaders="true" RetryInterval="60">
      <Server ConnectTimeout="0" ExtendedHandshake="false" MaxConnections="-1" Name="was7node1_TestServer" ServerIOTimeout="0" WaitForContinue="false">
         <Transport Hostname="HOST_NAME" Port="9361" Protocol="http"/>
         <Transport Hostname="HOST_NAME" Port="9561" Protocol="https">
            <Property Name="keyring" Value="/global/site/vendor/WAS/WebSphere7/AppServer/Plugin/etc/plugin-key.kdb"/>
            <Property Name="stashfile" Value="/global/site/vendor/WAS/WebSphere7/AppServer/Plugin/etc/plugin-key.sth"/>
         </Transport>
      </Server>

# awk -F\" '/<Server/{n=$8}/Port/&& $4 ~ port{print n,$4}' port=9361 data.sample
was7node1_TestServer 9361

# awk -F\" '/<Server/{n=$8}/Port/&& $4 ~ port{print n,$4}' port=9360 data.sample
was7node1_sample_server1 9360
bash-3.00$ cat plugin |nawk -F\" '/<Server/{n=$8}/Port/&& $4 ~ port{print n,$4}' port=9360 |awk '{print $1}'
was7node1_sample_server1
bash-3.00$ cat plugin |nawk -v port="9360" '$0 ~ port { print a[5];next } {split($0,a," ");next }'|sed '/^$/d'|awk -F\" '{print $2}'
was7node1_sample_server1

My real challenge starts from here..... :slight_smile:

Thanks both of you...!!

Regards
Abhi

This is UUoC !

# nawk -F\" '/<Server/{n=$8}/Port/&& $4 ~ port{print n}' port=9360 plugin
was7node1_sample_server1

okay...i guess i am too much used to 'cat'.i was just testing those commands and i used cat in them.

nevertheless ,i would like to see how we can extract same NAME element from below 2 syntaxes.

        <Server ConnectTimeout="0" ExtendedHandshake="false"
            MaxConnections="0" Name="HOST_server1" WaitForContinue="false">
            <Transport Hostname="HOST_NAME" Port="9098" Protocol="http"/>
            <Transport Hostname="HOST_NAME" Port="9053" Protocol="https">
                <Property Name="keyring" Value="/global/site/vendor/WebSphere5/DeploymentManager/etc/plugin-key.kdb"/>
                <Property Name="stashfile" Value="/global/site/vendor/WebSphere5/DeploymentManager/etc/plugin-key.sth"/>
                <Property Name="certLabel" Value="selfsigned"/>
            </Transport>
        </Server>

============================================================================

<Server ConnectTimeout="0" ExtendedHandshake="false"
            MaxConnections="0" Name="HOST_WebSphere_Portal" WaitForContinue="false">
            <Transport Hostname="HOST_NAME" Port="9198" Protocol="http"/>
            <Transport Hostname="HOST_NAME" Port="9153" Protocol="https">
                <Property Name="keyring" Value="/global/site/vendor/WebSphere5/DeploymentManager/etc/plugin-key.kdb"/>
                <Property Name="stashfile" Value="/global/site/vendor/WebSphere5/DeploymentManager/etc/plugin-key.sth"/>
                <Property Name="certLabel" Value="selfsigned"/>
            </Transport>
            <Transport Hostname="HOST_NAME" Port="9090" Protocol="http"/>
            <Transport Hostname="HOST_NAME" Port="9043" Protocol="https">
                <Property Name="keyring" Value="/global/site/vendor/WebSphere5/DeploymentManager/etc/plugin-key.kdb"/>
                <Property Name="stashfile" Value="/global/site/vendor/WebSphere5/DeploymentManager/etc/plugin-key.sth"/>
                <Property Name="certLabel" Value="selfsigned"/>
            </Transport>
        </Server>
# cat file
<Server ConnectTimeout="0" ExtendedHandshake="false"
            MaxConnections="0" Name="HOST_WebSphere_Portal" WaitForContinue="false">
            <Transport Hostname="HOST_NAME" Port="9198" Protocol="http"/>
            <Transport Hostname="HOST_NAME" Port="9153" Protocol="https">
                <Property Name="keyring" Value="/global/site/vendor/WebSphere5/DeploymentManager/etc/plugin-key.kdb"/>
                <Property Name="stashfile" Value="/global/site/vendor/WebSphere5/DeploymentManager/etc/plugin-key.sth"/>
                <Property Name="certLabel" Value="selfsigned"/>
            </Transport>
            <Transport Hostname="HOST_NAME" Port="9090" Protocol="http"/>
            <Transport Hostname="HOST_NAME" Port="9043" Protocol="https">
                <Property Name="keyring" Value="/global/site/vendor/WebSphere5/DeploymentManager/etc/plugin-key.kdb"/>
                <Property Name="stashfile" Value="/global/site/vendor/WebSphere5/DeploymentManager/etc/plugin-key.sth"/>
                <Property Name="certLabel" Value="selfsigned"/>
            </Transport>
        </Server>

# awk -F\"  '/<Server/{f=1}f{for(i=1;++i<=NF;){if($i==" Name=") n=$(i+1)}}f && n{for(i=1;++i<=NF;){if($i==" Port=" && $(i+1)==p){print n;f=0}}}' p=9198 file
HOST_WebSphere_Portal

thanks Mate....!! you have helped when it mattered most.....!!

i just need your confirmation on following....

bash-2.05$ cat file
    <ServerGroup Name="HOST/vignlive">
        <Server CloneID="10t8ubf9r" Name="vignlive">
            <Transport Hostname="HOST" Port="9082" Protocol="http"/>
        </Server>
    </ServerGroup>

bash-2.05$ nawk -F\" '/Server/{n=$4}/Port/&& $4 ~ port{print n,$4}' port=9082 file|awk '{print $1}'
vignlive

Is it correct ?

Regards
Abhi