我在文件中有以下文本块:
test3.legacy test4.legacy test3 test3.kami
我只想test3
整体搜索,什么也不要替换。不幸的是,我所有的尝试都从和中删除了test3。我试过了:test3.legacy
test3.kami
sed 's/^test3://g' myfile.txt
sed 's/\btest3\b//g' myfile.txt
sed 's/\<test3\>//g' myfile.txt
没有任何运气。有什么想法可以解决这个问题吗?
编辑:大多数尝试导致以下结果:.legacy test4.legacy .kami
这是你想要的吗?
$ sed 's/\(^\| \)test3\( \|$\)/\1/g' file
test3.legacy test4.legacy test3.kami
这个说
substitute
(^ start of line OR space)
test3
(space OR end of line)
with match 1 (AKA space or start of line)
更新:
就像好@Stephane Chazelas所说的那样,这在某些情况下是无法解决的。还强调可移植性部分。请参阅下面的答案。
GNU变体可以(希望)是:
sed 's/\(^\| \)\(test3\( \|$\)\)*/\1/g'
# Or perhaps:
sed 's/\(^\| \)\(test3\( \|$\)\)\+/\1/g'
照顾重复的比赛。可选地,一个人也可以照顾多个空间等。取决于输入。
欧佩德
作为替代方案(仅作为起点):
sed 's/\Wtest3\W/ /g'
我不认为这是:
$ sed 's/test3\.\?[^ ]* *//g' file
test4.legacy
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句