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

时间:2015-11-03 16:29:01

标签: unix awk sed grep

<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如果它是OText.Negotiate,我需要得到Negociere。

如何在脚本文件中执行此操作。我正在寻找一个使用sed / awk / grep的答案 感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

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

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

//trans-unit[@id='OText.Meetwithcustomer']/target字符串称为 XPath表达式。有许多工具支持XPath。

答案 1 :(得分:0)

非健壮的awk黑客

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


\u015eedin\u0163\u0103 cu clientul

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