Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sed 与&;(安培)_Sed - Fatal编程技术网

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 `\'.