0

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

2
  • Does the second grey block represent the output your are looking for? Are you trying to make another file? Or load some variables from the XML? Commented Apr 7, 2014 at 9:11
  • Second block is the ouput that I want, basicliy I need this values in variables, so that I can display them, or work with them later... Commented Apr 7, 2014 at 9:13

2 Answers 2

2

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...

Sign up to request clarification or add additional context in comments.

2 Comments

Ok thanks, is it possible to assign this values to variables, in while/for loop, because I need this in values in var, to work with them later?
@user3319356 xslt gives the output, you can define the format in xslt, and if you want to use any of them, either do it in xslt, or using grep to catch that. And in xslt we cannot assign text to your bash var.
1

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.

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

3 Comments

take a look the xml example format, it is really messy, I guess the xml could be generated by some program... if the xml format could be changed, awk/sed/... those text processing tools are not the right choice. Also awk/sed.. have no idea about xml context( element, attribute, the hierarchy...) e.g. your cmd would be fail if the whole xml in one single line. If we make the awk fit any format of a xml, we have almost written a xml parser... I admit that for fixed format xml, awk/sed/grep could be used as quick and dirty (but not safe) solution, I do that sometimes too.
@Kent: I agree, completely, that's why I said it may get the OP close and it may be ok for a one-off, or a quick way to solve a problem that is not of primary interest but that just needs solving to get somewhere else and sort out the larger picture.
I have added a way of loading the output into variables - well, an array really.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.