[英]Working with sed linux command
在我的shellscript
代码中,我看到有一行正在使用sed
命令处理Telephone
号码。
sed "s~<Telephone type[ ]*=[ ]*\"fax\"[ ]*><Number>none[ ]*</Number></Telephone>~~g" input.xml > output.xml
我不了解正则表达式的实际作用。
<Telephone type[ ]*=[ ]*\"fax\"[ ]*><Number>none[ ]*</Number></Telephone>
我正在做工程,以使其正常工作。
我的xml结构如下。
<ContactMethod>
<InternetEmailAddress>donald.francis@lexisnexis.com</InternetEmailAddress>
<Telephone type = "work">
<Number>215-639-9000 x3281</Number>
</Telephone>
<Telephone type = "home">
<Number>484-231-1141</Number>
</Telephone>
<Telephone type = "fax">
<Number>N/A</Number>
</Telephone>
<Telephone type = "work">
<Number>215-639-9000 x3281</Number>
</Telephone>
<Telephone type = "home">
<Number>484-231-1141</Number>
</Telephone>
<Telephone type = "fax">
<Number>none</Number>
</Telephone>
<Telephone type1 = "fax12234">
<Number>484-231-1141sadsadasdasdaasd</Number>
</Telephone>
</ContactMethod>
该正则表达式可识别<Telephone type = "fax">
条目,其中编号为none
条目将被删除。
分解:
s
为“取代” sed命令。
~
模式分隔符。 您可以为此选择任何字符。 sed重新识别它是因为它紧随s
。
<Telephone type
匹配文字文本“ <电话类型”。
[ ]*
匹配零个或多个空格。
=
匹配文字“ =“
[ ]*
匹配零个或多个空格。
\\"fax\\"
匹配文字。 因为整个模式都出现在引号内,所以引号被转义,但是在sed看到它们之前,shell删除了引号字符( \\
)。
[ ]*
匹配零个或多个空格。
><Number>none
匹配文字的文本。
[ ]*
匹配零个或多个空格。
</Number></Telephone>
与文字文本匹配。
~~
模式分隔符结束搜索模式,并包围一个空的替换模式。
g
是一个标志,表示替换将在每行上执行多次。
唯一令我困惑的是,该模式将不匹配任何包含换行符的内容,因此我认为您的input.xml
格式实际上不像示例数据中的格式吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.