2015-11-03 94 views
0
<trans-unit id="OText.Meetwithcustomer"> 
      <source>Meet with customer</source> 
      <target>\u015eedin\u0163\u0103 cu clientul 
</target> 
      <note>A step in the sales stage of type qualification in a bid and in a project.</note> 
      <note>ID:240645::TYPE:Text/Data</note> 
     </trans-unit> 
     <trans-unit id="OText.Negotiate"> 
      <source>Negotiate</source> 
      <target>Negociere</target> 
      <note>A step in the sales stage of type closed in a standard and in a project.</note> 
      <note>ID:240646::TYPE:Text/Data</note> 
     </trans-unit> 

我正在將trans-unit id傳遞給腳本,並且在腳本內部,我試圖獲取該trans-unit id的目標標記值。 trans-unit id值可以是OText.Meetwithcustomer或OText.Negotiate。如果是OText.Meetwithcustomer,則需要獲取值\ u015eedin \ u016​​3 \ u0103 cu clientul,如果是OText.Negotiate,則需要獲取Negociere。如何提取unix中兩個標記之間的值

如何在腳本文件中執行此操作。我正在尋找一個使用sed/awk/grep的答案感謝您的幫助。

+0

您的示例與您的陳述相矛盾' Negociere' – karakfa

+0

我將傳遞單元ID傳遞給腳本,並在腳本內部,我試圖獲取該傳輸單元ID的目標標記值。 trans-unit id值可以是OText.Meetwithcustomer或OText.Negotiate。如果是OText.Meetwithcustomer,則需要獲取值\ u015eedin \ u016​​3 \ u0103 cu clientul,如果是OText.Negotiate,則需要獲取Negociere。 – arun

+0

[從簡單的XML文件中提取數據]的可能的副本(http://stackoverflow.com/questions/2222150/extraction-of-data-from-a-simple-xml-file) – tripleee

回答

1

使用XML感知工具來解析和處理XML。例如,xsh

open file.xml ; 
echo //trans-unit[@id='OText.Meetwithcustomer']/target ; 

//trans-unit[@id='OText.Meetwithcustomer']/target串稱爲XPath表達式。有很多支持XPath的工具。

+0

Thanks.Could you please share一個不使用任何XML解析器的答案? – arun

+1

@arun:關鍵是你不需要它。它很脆弱,不可靠,而且難以維護。使用支持XML的工具是最好的選擇。 – choroba

0

非健壯awk破解

$ awk -v RS="</trans-unit>" '/OText.Meetwithcustomer/' file 
| awk -v FS="<target>" 'NF>1{print $2}' 


\u015eedin\u0163\u0103 cu clientul 

說明:基於XML結構提取與搜索詞的記錄。再次從該記錄中捕獲目標標籤旁邊的文本。你可以合併腳本,但我認爲這樣更好。

相關問題