从xml文件中提取值bash

时间:2014-04-07 08:44:02

标签: xml bash parsing

我需要帮助解析shell脚本中的xml文件,我需要从给定的xml文件中提取值,并将它们放到变量中。这是xml文件的一部分

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

所以我需要为每个Sector数据下的examle,cellIdentity值提取一些值

Sector 1
VAR1=461
VAR2=465
VAR3=468

Sector 2
VAR4=462
VAR5=466
VAR6=469

Sector 3
VAR7=463
VAR8=467
VAR9=460

我尝试使用xmllint但是我遇到了一些错误:

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

我对xmllint并不熟悉,但我需要看看它是如何工作的,以便我可以从文件中提取其他值。 感谢

2 个答案:

答案 0 :(得分:2)

xslt是满足您需求的正确工具。通常你可以在你的linux机器上找到一个名为xsltproc的工具。你需要写一个简短的xslt文件:

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

然后:

 xsltproc 1.xslt 1.xml

给你:

Sector:1
461
465
468




Sector:2
462
466
469




Sector:3
463
467
460

BTW,你问题中的xml示例格式不正确。 </Site>遗失了......

答案 1 :(得分:1)

* 强文 *这可能会让你关闭:

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

输出:

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

如果看到sectorNumber,则会使用字段2中的扇区号创建输出变量x。如果看到cellIdentity,则会拾取并附加到{{1}带分隔符。如果遇到x,则打印输出变量。

EDITED

如果你想要变量或更好的数组</Sector中的值,你可以这样做:

a