user3319356
user3319356

Reputation: 173

Extract values from xml file, bash

I need help with parsing xml file in shell script, I need to extract values from the given xml file, and put them to variables. Here is the part of xml file

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE Site SYSTEM "SiteEquipment.dtd"  >

<!--    Site Equipment Configuration   -->



<Site>


<Format

revision="AK5"

/>


<OptionalEquipmentConfiguration

configureSau="NO"

absoluteTimeSynchEnabled="NO"

gpsOutEnabled="FALSE"

smokeDetector="FALSE"

/>


<SiteLocationConfiguration

siteName="Alzey002"

logicalName="FXU046"

>


<SectorData

sectorNumber="1"

latitude="4635826"

latHemisphere="NORTH"

longitude="377963"

geoDatum="DHDN-"

beamDirection="060"

height="3000"

sectorGroup="-1"

/>


<SectorData

sectorNumber="2"

latitude="4635826"

latHemisphere="NORTH"

longitude="377963"

geoDatum="DHDN-"

beamDirection="140"

height="3000"

sectorGroup="-1"

/>


<SectorData

sectorNumber="3"

latitude="4635826"

latHemisphere="NORTH"

longitude="377963"

geoDatum="DHDN-"

beamDirection="300"

height="3000"

sectorGroup="-1"

/>

</SiteLocationConfiguration>


<SectorCapabilitySettings>


<SectorCapability

radioBuildingBlock="RBB12_1A"

cpriLineRate="Ox4"

sectorNumber="1"

primaryPortId="BU1_A"

sectorSequenceNumber="1"

rruInstalled="NO"

/>


<SectorCapability

radioBuildingBlock="RBB12_1A"

cpriLineRate="Ox4"

sectorNumber="2"

primaryPortId="BU1_B"

sectorSequenceNumber="1"

rruInstalled="NO"

/>


<SectorCapability

radioBuildingBlock="RBB12_1A"

cpriLineRate="Ox4"

sectorNumber="3"

primaryPortId="BU1_C"

sectorSequenceNumber="1"

rruInstalled="NO"

/>

</SectorCapabilitySettings>


<SectorEquipmentConfiguration>


<TmaConfiguration>


<TmaSector

sectorNumber="1"

tmaType="NONE"

tmaType2="NONE"

tmaType3="NONE"

typeOfRet="RETU"

typeOfRet2="NONE"

typeOfRet3="NONE"

riuInstalled="NO"

riuInstalled2="NO"

currentLowSupervision_A="ON"

currentLowSupervision_B="ON"

currentLowSupervision_C="ON"

currentLowSupervision_D="ON"

/>


<TmaSector

sectorNumber="2"

tmaType="NONE"

tmaType2="NONE"

tmaType3="NONE"

typeOfRet="RETU"

typeOfRet2="NONE"

typeOfRet3="NONE"

riuInstalled="NO"

riuInstalled2="NO"

currentLowSupervision_A="ON"

currentLowSupervision_B="ON"

currentLowSupervision_C="ON"

currentLowSupervision_D="ON"

/>


<TmaSector

sectorNumber="3"

tmaType="NONE"

tmaType2="NONE"

tmaType3="NONE"

typeOfRet="RETU"

typeOfRet2="NONE"

typeOfRet3="NONE"

riuInstalled="NO"

riuInstalled2="NO"

currentLowSupervision_A="ON"

currentLowSupervision_B="ON"

currentLowSupervision_C="ON"

currentLowSupervision_D="ON"

/>

</TmaConfiguration>


<AntennaConfiguration>


<AntennaSector

sectorNumber="1"

antennaType="2"

antennaType2="0"

antennaType3="0"

mechanicalTilt="0"

mechanicalTilt2="0"

mechanicalTilt3="0"

electricalTilt="30"

band="1"

fqBandHighEdgeBranchA="21250"

fqBandLowEdgeBranchA="21100"

fqBandHighEdgeBranchB="21250"

fqBandLowEdgeBranchB="21100"

fqBandHighEdgeBranchC=""

fqBandLowEdgeBranchC=""

fqBandHighEdgeBranchD=""

fqBandLowEdgeBranchD=""

fqBandHighEdgeBranchE=""

fqBandLowEdgeBranchE=""

fqBandHighEdgeBranchF=""

fqBandLowEdgeBranchF=""

dlFeederAttenuationBranchA="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

ulFeederAttenuationBranchA="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

dlFeederDelayBranchA="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

ulFeederDelayBranchA="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

dlFeederAttenuationBranchB="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

ulFeederAttenuationBranchB="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

dlFeederDelayBranchB="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

ulFeederDelayBranchB="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

sectorOutputPower="60"

beamDirection="060"

beamDirection2="000"

beamDirection3="000"

/>


<AntennaSector

sectorNumber="2"

antennaType="2"

antennaType2="0"

antennaType3="0"

mechanicalTilt="0"

mechanicalTilt2="0"

mechanicalTilt3="0"

electricalTilt="0"

band="1"

fqBandHighEdgeBranchA="21250"

fqBandLowEdgeBranchA="21100"

fqBandHighEdgeBranchB="21250"

fqBandLowEdgeBranchB="21100"

fqBandHighEdgeBranchC=""

fqBandLowEdgeBranchC=""

fqBandHighEdgeBranchD=""

fqBandLowEdgeBranchD=""

fqBandHighEdgeBranchE=""

fqBandLowEdgeBranchE=""

fqBandHighEdgeBranchF=""

fqBandLowEdgeBranchF=""

dlFeederAttenuationBranchA="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

ulFeederAttenuationBranchA="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

dlFeederDelayBranchA="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

ulFeederDelayBranchA="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

dlFeederAttenuationBranchB="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

ulFeederAttenuationBranchB="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

dlFeederDelayBranchB="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

ulFeederDelayBranchB="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

sectorOutputPower="60"

beamDirection="140"

beamDirection2="000"

beamDirection3="000"

/>


<AntennaSector

sectorNumber="3"

antennaType="2"

antennaType2="0"

antennaType3="0"

mechanicalTilt="0"

mechanicalTilt2="0"

mechanicalTilt3="0"

electricalTilt="60"

band="1"

fqBandHighEdgeBranchA="21250"

fqBandLowEdgeBranchA="21100"

fqBandHighEdgeBranchB="21250"

fqBandLowEdgeBranchB="21100"

fqBandHighEdgeBranchC=""

fqBandLowEdgeBranchC=""

fqBandHighEdgeBranchD=""

fqBandLowEdgeBranchD=""

fqBandHighEdgeBranchE=""

fqBandLowEdgeBranchE=""

fqBandHighEdgeBranchF=""

fqBandLowEdgeBranchF=""

dlFeederAttenuationBranchA="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

ulFeederAttenuationBranchA="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

dlFeederDelayBranchA="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

ulFeederDelayBranchA="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

dlFeederAttenuationBranchB="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

ulFeederAttenuationBranchB="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

dlFeederDelayBranchB="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

ulFeederDelayBranchB="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1"

sectorOutputPower="60"

beamDirection="300"

beamDirection2="000"

beamDirection3="000"

/>

</AntennaConfiguration>


<InitiateSectorsConfiguration>


<InitiatedSector

sectorNumber="1"

antennaSupervisionBranchA="10"

antennaSupervisionBranchB="10"

antennaSupervisionBranchC="0"

antennaSupervisionBranchD="0"

antennaSupervisionBranchE="0"

antennaSupervisionBranchF="0"

/>


<InitiatedSector

sectorNumber="2"

antennaSupervisionBranchA="10"

antennaSupervisionBranchB="10"

antennaSupervisionBranchC="0"

antennaSupervisionBranchD="0"

antennaSupervisionBranchE="0"

antennaSupervisionBranchF="0"

/>


<InitiatedSector

sectorNumber="3"

antennaSupervisionBranchA="10"

antennaSupervisionBranchB="10"

antennaSupervisionBranchC="0"

antennaSupervisionBranchD="0"

antennaSupervisionBranchE="0"

antennaSupervisionBranchF="0"

/>

</InitiateSectorsConfiguration>


<LocalCellConfiguration

carrierAllocationMode="Flexible"

>


<Sector

sectorNumber="1"

>


<Cell

cellNumber="1"

cellCreated="YES"

cellIdentity="461"

cellRange="35000"

baseBandPoolId="2"

numberOfTxBranches="1"

numberOfRxBranches="2"

/>


<Cell

cellNumber="2"

cellCreated="YES"

cellIdentity="465"

cellRange="35000"

baseBandPoolId="2"

numberOfTxBranches="1"

numberOfRxBranches="2"

/>


<Cell

cellNumber="3"

cellCreated="YES"

cellIdentity="468"

cellRange="35000"

baseBandPoolId="1"

numberOfTxBranches="1"

numberOfRxBranches="2"

/>

</Sector>


<Sector

sectorNumber="2"

>


<Cell

cellNumber="1"

cellCreated="YES"

cellIdentity="462"

cellRange="35000"

baseBandPoolId="2"

numberOfTxBranches="1"

numberOfRxBranches="2"

/>


<Cell

cellNumber="2"

cellCreated="YES"

cellIdentity="466"

cellRange="35000"

baseBandPoolId="2"

numberOfTxBranches="1"

numberOfRxBranches="2"

/>


<Cell

cellNumber="3"

cellCreated="YES"

cellIdentity="469"

cellRange="35000"

baseBandPoolId="1"

numberOfTxBranches="1"

numberOfRxBranches="2"

/>

</Sector>


<Sector

sectorNumber="3"

>


<Cell

cellNumber="1"

cellCreated="YES"

cellIdentity="463"

cellRange="35000"

baseBandPoolId="2"

numberOfTxBranches="1"

numberOfRxBranches="2"

/>


<Cell

cellNumber="2"

cellCreated="YES"

cellIdentity="467"

cellRange="35000"

baseBandPoolId="2"

numberOfTxBranches="1"

numberOfRxBranches="2"

/>


<Cell

cellNumber="3"

cellCreated="YES"

cellIdentity="460"

cellRange="35000"

baseBandPoolId="1"

numberOfTxBranches="1"

numberOfRxBranches="2"

/>

</Sector>

</LocalCellConfiguration>

</SectorEquipmentConfiguration

So I need to extract some values for examle, cellIdentity value's under each Sector data

Sector 1
VAR1=461
VAR2=465
VAR3=468

Sector 2
VAR4=462
VAR5=466
VAR6=469

Sector 3
VAR7=463
VAR8=467
VAR9=460

I tried with xmllint but I get some errors:

 /usr/bin/xmllint --shell 1.xml <<<"cat/<Site/LocalCellConfiguration/Sector/Cell/cellIdentity/" | grep -v "^/ >"

Well I'm not so familiar with xmllint, but I need to see how it's working so that I can extract other values from the file. Thank

Upvotes: 0

Views: 282

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207345

*strong text*This might get you close:

awk -F\" '/sectorNumber/{x="Sector:" $2 RS} /cellIdentity/{x=x $2 RS} /\<\/Sector/{printf x}' file

Output:

Sector:1
461
465
468
Sector:2
462
466
469
Sector:3
463
467
460

If sectorNumber is seen, an output variable x is created with the sector number from field 2. If a cellIdentity is seen, that is picked up and appended to x with a separator. If </Sector is encountered, the output variable is printed.

EDITED

If you want the values in variables, or better, an array a, you could do this:

a=($(awk -F\" '/sectorNumber/{x=$2 RS}/cellIdentity/{x=x $2 RS}/\<\/Sector/{printf x}' file))
echo ${a[0]}
3
echo ${a[1]}
1
echo ${a[2]}
461
echo ${a[3]}
465

Upvotes: 1

Kent
Kent

Reputation: 195029

xslt is the right tool for your needs. usually you can find a tool called xsltproc on your linux box. You need write a short xslt file:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="//Sector[Cell/@cellIdentity]">
Sector:<xsl:value-of select="@sectorNumber"/>
<xsl:text>&#xa;</xsl:text>
      <xsl:for-each select="Cell">
          <xsl:value-of select="@cellIdentity"/>
          <xsl:text>&#xa;</xsl:text>
      </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

then:

 xsltproc 1.xslt 1.xml

gives you:

Sector:1
461
465
468




Sector:2
462
466
469




Sector:3
463
467
460

BTW, the xml example in your question is not well-formed. </Site> is missing...

Upvotes: 2

Related Questions