2013-06-26 62 views
0

我需要從bash腳本中的xml文件獲取屬性,但我既不能使用xmllint --xpath,也不能使用xmlstarlet,因爲它們在我工作的服務器上不可用。訪問bash腳本中的xml屬性

我已經嘗試過使用grep,cut和sed的解決方案,但它在很長一段時間裏並不是一個好的解決方案。

沒有可用grep_xml機器上,我可以存取權限與它的元素,但是當我試圖訪問我的屬性我得到「在處理程序無法識別的錯誤表達」

,這是我的xml文件

<?xml version="1.0" standalone="yes"?> 
<p4codeline xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="p4codeline_1_1.xsd"> 
    <module name="kpreader" currentVersion="kpreader_1.1STD0" previousVersion="kpreader_1.0STD0"> 
    <codeline owner="undefined" path="//HEP/jcas/kpreader/trunk/..."> 
     <namingConvention/> 
     <description>Main codeline for development</description> 
     <rules> 
     <rule>Develop on MAIN, and create a TAG codeline on release</rule> 
     <rule>Never broke the build on the MAIN</rule> 
     </rules> 
    </codeline> 
    <externals> 
     <external viewPath="J2ep_BuildTools/..." codeLine="//CT/JAVA/J2ep_BuildTools/Source/tags/J2EP_BUILDTOOLS_1.6STD0/..." depotPath="."/> 
    </externals> 
    </module> 
</p4codeline> 

我只需要使用基於bash或命令的解決方案訪問代碼行中的路徑屬性。

我已經試過類似

xml_grep -t '/p4codeline/module/codeline/@path' file.xml 

它answear我

error: unrecognized expression in handler: '/p4codeline/module/[email protected]' at /usr/bin/xml_grep line 183 
+3

是,不要把我們留在黑暗中。如果你去看醫生,不要只說「哎呀!」。請明確點。 – Jens

+0

什麼是輸入,到目前爲止您嘗試了什麼,它打印了什麼,以及您期望它(或解決方案)打印的內容是什麼?我們需要更多細節。 – l0b0

+0

爲此,我將使用Perl +您選擇的XML解析器。 – m0skit0

回答

1

由於這些東西通常會去,這個命令是高度依賴於你的輸入

$ awk '/path/ {print $4}' FS='"' file.xml 
//HEP/jcas/kpreader/trunk/...