Sed 与&;(安培)
正在尝试清理一些看起来像Sed 与&;(安培),sed,Sed,正在尝试清理一些看起来像 Forest & Paper Products Manufacturing 使用sed命令,如 sed "s/ \& / & /" 但是一旦sed处理完这个文件,我的输出看起来像 Forest & amp; Paper Products Manufacturing 不明白为什么sed在& 我可以用以下方法解决这个问题: sed "s/ \& / \& /" 但是为什么我需要在替换部件中引用带有\前
Forest & Paper Products Manufacturing
使用sed命令,如
sed "s/ \& / & /"
但是一旦sed处理完这个文件,我的输出看起来像
Forest & amp; Paper Products Manufacturing
不明白为什么sed在&
我可以用以下方法解决这个问题:
sed "s/ \& / \& /"
但是为什么我需要在替换部件中引用带有\前缀的&,
&
表示匹配的字符串\0
。所以,如果您想在替换部件中使用literal&
,则必须对其进行转义
例如
从sed信息页面获取的相关信息:
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
及
但是如果您事先不知道替换文本,如何用\&替换?我需要先替换替换文本,然后才能将其替换为sed?:D
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
To include a literal `\', `&', or newline in the final replacement, be
sure to precede the desired `\', `&', or newline in the REPLACEMENT with
a `\'.