Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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,纯学术,但这让我很沮丧 我要更正此文本: there there are are multiple lexical errors in this line line 使用sed。我已经做到了这一点: sed 's/\([a-z][a-z]*[ ,\n][ ,\n]*\)\1/\1/g' < file.text sed大师能解释一下为什么上面没有处理结尾的单词吗?这是因为在最后一种情况下(line),您的正则表达式内存1将包含line(line后跟空格),您正在搜索它的重复。由于最后一行

纯学术,但这让我很沮丧

我要更正此文本:

there there are are multiple lexical errors in this line line
使用sed。我已经做到了这一点:

sed 's/\([a-z][a-z]*[ ,\n][ ,\n]*\)\1/\1/g' < file.text

sed大师能解释一下为什么上面没有处理结尾的单词吗?

这是因为在最后一种情况下(
line
),您的正则表达式内存1将包含
line
(line后跟空格),您正在搜索它的重复。由于最后一行
后没有空格
匹配失败

若要解决此问题,请在结尾单词
后添加空格

或者,您可以将正则表达式更改为:

sed -e 's/\b\([a-z]\+\)[ ,\n]\1/\1/g'

N.B.RE-
[,\N]
sed使用
\N
作为行分隔符。因此,除非您在模式空间中插入
\n
,否则在将一行读入模式空间后将永远不会遇到它们。
sed -e 's/\b\([a-z]\+\)[ ,\n]\1/\1/g'