2012-10-19 62 views
0

我正在嘗試使批處理文件將數據拉出文件並將其設置爲變量。批量讀取部分xml文件

棘手的部分是我需要讀取XML文件,而我只需要下面一行的引號之間的數據...

narrative="I only need this text here" 

在該行的文本還可以包含空格,括號,斜槓,破折號和冒號。

示例XML文件

<?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
    <cadcall> 
     <call callnumber="123456" jurisdiction="abcd" department="dept 1" complaint="cost" priority="M" calltencode="" callername="Persons Name" phonenumber="Cell Number HERE" narrative="[10/02/2012 14:56:27 : pos9 : PERSON] Fairly long narrative here describing issue of dispatch, but sometimes these can be short." alarmtype="" ocanumber="0000000000" disposition1="TRAN" /> 
    </cadcall> 

回答

1

正確的工具來做到這一點是從libxmlxmllint,請提供一個更完整的XML例子,我會告訴你如何在XML使用Xpath請求。

編輯

這裏使用XPath的解決方案(一點點黑客:contains):

xmllint --xpath '/cadcall/call/@narrative[contains(.,'.')]' file.xml 
+0

這是一個完整的例子。 <?xml version =「1.0」encoding =「utf-8」standalone =「yes」?> -

+0

在沒有'cut'的情況下添加'xmllint'命令 –

+0

當我運行這個時,出現XPath錯誤:表達式無效。它顯示了一個指向該時期的卡爾特; (X,'。')]' –

0

沒有看到完整的輸入,只是根據你的榜樣行。 grep適合你。

kent$ echo 'narrative="I only need this text here"'|grep -Po '(?<=narrative=")[^"]*' 
I only need this text here