2014-04-07 85 views
0

我需要幫助解析shell腳本中的xml文件,我需要從給定的xml文件中提取值,並將它們放到變量中。下面是XML文件的從xml文件中提取值bash

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

所以我需要提取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,但我需要看看它是如何工作的,以便我可以從文件中提取其他值。 謝謝

+0

是否爲第二灰度塊代表你正在尋找的輸出?你想製作另一個文件嗎?或從XML加載一些變量? –

+0

第二個塊是我想要的輸出,basicliy我需要這些值在變量中,以便我可以顯示它們,或稍後使用它們...... – user3319356

回答

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看出,輸出變量x與創建來自字段2的扇區號。如果看到cellIdentity,則拾取並用分隔符將其附加到x。如果遇到</Sector,則輸出變量將被打印。

EDITED

如果你想在變量的值,或更好的,數組a,你可以這樣做:

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 
+0

看看xml的例子格式,它真的很亂,我估計xml可以通過某些程序生成...如果xml格式可以改變,awk/sed/...那些文本處理工具不是正確的選擇。還awk/sed ..不知道xml上下文(元素,屬性,層次結構...)例如如果整行xml在一行中,你的cmd將會失敗。如果我們使awk適合xml的任何格式,我們幾乎寫了一個xml解析器......我承認,對於固定格式xml,awk/sed/grep可以用作快速和骯髒(但不安全)的解決方案,我有時候也這樣做。 – Kent

+0

@Kent:我完全同意這一點,這就是爲什麼我說這可能會讓OP關閉,並且可能是一次性或快速解決問題的方法,但這只是一個不重要的問題,但只需要解決去別的地方找出更大的圖片。 –

+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 

順便說一句,在你的問題中的XML示例是沒有很好地形成。 </Site>缺少...

+0

好的,謝謝,是否可以將這個值賦給變量,在while/for循環,因爲我需要var中的值來處理它們呢? – user3319356

+0

@ user3319356 xslt給出了輸出,你可以在xslt中定義格式,如果你想使用它們中的任何一個,可以在xslt中使用,或者使用grep來捕獲它。而在xslt中,我們無法將文本分配給您的bash var。 – Kent