Search code examples
bashawkgrepstdoutcut

Extract a string from a returned API call in XML


I would like to take XML API output and extract the name (name="vPC-NAME") from a file. Since this call returns multiple names I need to be able to search for any text between the double quotes.

I have tried

awk 'name="*"' PCMappings_Result2.xml

cat file | egrep -o 'name=\"*\"




<pcAggrIf activePorts="1" adminSt="up" autoNeg="on" bw="0" childAction="" createTime="18074:07:16:08.069" ctrl="fast-sel-hot-stdby,graceful-conv,susp-individual" delay="1" descr="" dn="node-101/sys/aggr-[po4]" dot1qEtherType="0x8100" ethpmCfgFailedBmp="" ethpmCfgFailedTs="00:00:00:00.000" ethpmCfgState="0" fcotChannelNumber="Channel32" fop="eth1/45" hashDist="adaptive" id="po4" inhBw="unspecified" iod="67" isPlatformSupported="yes" isReflectiveRelayCfgSupported="Supported" lastBundleMbr="eth1/47" lastBundleTime="18074:07:16:08.318" lastSt="successful" lastStCause="" lastTime="18074:07:16:08.318" lastUnbundleMbr="unspecified" lastUnbundleTime="00:00:00:00.000" layer="Layer2" lcOwn="local" lif="0" linkDebounce="100" linkLog="default" loadDeferStartTime="00:00:00:00.000" ltl="8198" maxActive="16" maxLinks="16" mdix="auto" medium="broadcast" minLinks="1" modTs="2019-06-27T07:51:04.547+00:00" mode="trunk" monPolDn="uni/infra/moninfra-default" mtu="9000" name="vPC-NAME"/>

Solution

  • You should proper tool for XML, but if no option are available, this will work:

    awk -F'name="' 'NF>1{split($2,a,"\"");print a[1]}' file
    vPC-NAME
    
    • Divide the line by name=" as Field Separator
    • And if line has at least two fields NF>1 do:
    • Split last part $2 by " and store it in array a
    • Print first part of array a[1]

    If there are multiple name in one line, we need to loop trough the line:

    awk '{for (i=1;i<=NF;i++) if ($i~/^name/) {split($i,a,"\"");print a[2]}}' file
    n3k-p45
    esxi2-vpc