如何提取unix中两个标记之间的值

如何提取unix中两个标记之间的值

问题描述:

<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://*.com/questions/2222150/extraction-of-data-from-a-simple-xml-file) – tripleee

使用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

非健壮awk破解

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


\u015eedin\u0163\u0103 cu clientul 

说明:基于XML结构提取与搜索词的记录。再次从该记录中捕获目标标签旁边的文本。你可以合并脚本,但我认为这样更好。