D.Z*_*Zou 0 sed regular-expression
我正在练习我的 sed 和正则表达式,并试图在下面的文件中用 lalaland 替换所有“cheer”实例,同时排除欢呼之类的东西:
ubuntu@ip-172-31-58-40:~$ cat test
cheer
cheering
.
word
cheerytree
acheerytree
ubuntu@ip-172-31-58-40:~$ sed -i "s/cheer[^ing]/lalaland/g" test
ubuntu@ip-172-31-58-40:~$ cat te
cat: te: No such file or directory
ubuntu@ip-172-31-58-40:~$ cat test
cheer
cheering
.
word
lalalandtree
alalalandtree
Run Code Online (Sandbox Code Playgroud)
我不明白为什么要留下第一个项目,欢呼。
谢谢
该模式[^ing]
不会做你认为它的作用:它一个字符,它必须是没有的匹配i
,n
或g
。
如果您的问题可以重新表述为“将欢呼的实例替换为整个单词”,那么以下可能有效,使用标记作为单词边界,\b
:
sed 's/\bcheer\b/lalaland/g'
Run Code Online (Sandbox Code Playgroud)