尝试清理一些看起来像这样的 xml 文本
Forest & Paper Products Manufacturing
使用 sed 命令,例如
sed "s/ \& / & /"
但是一旦 sed 处理完文件,我的输出看起来像
Forest & amp; Paper Products Manufacturing
无法弄清楚为什么 sed 在 & 之后放置一个空格
我可以用以下方法解决这个问题:
sed "s/ \& / \& /"
但为什么我需要用 \ 前缀引用 &
在替换部分中,&
表示匹配的字符串\0
。所以如果你想要字面意思&
在替换部分,你必须逃脱它。
E.g
kent$ (master|✔) echo "foo"|sed 's/.*/&_bar/'
foo_bar
kent$ (master|✔) echo "foo"|sed 's/.*/\&_bar/'
&_bar
相关信息取自 sed 的 INFO 页面:
The REPLACEMENT can contain `\N' (N being a number from 1 to 9,
inclusive) references, which refer to the portion of the match which
is contained between the Nth `\(' and its matching `\)'. Also, the
REPLACEMENT can contain unescaped `&' characters which reference the
whole matched portion of the pattern space
and
To include a literal `\', `&', or newline in the final replacement, be
sure to precede the desired `\', `&', or newline in the REPLACEMENT with
a `\'.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)