我正在尝试修改文本文件的几行。
# cat example.txt
tested
tests
testing
如果“test”后面没有跟“ed”或“s”,则将其更改为“work”。以下表达式按预期工作:
test(?!ed|s)
但它在 sed 中不能像这样工作...
# sed -r 's/test\(?!ed\|s\)/work/g' example.txt
tested
tests
testing
预期的输出是:
tested
tests
working
我猜 sed 不支持前瞻或回顾。还有其他简单的 linux 命令吗?
最佳答案
perl -pe's/test(?!ed|s)/work/g' filename
对于 Unicode(非 ASCII)文本,我们需要启用对它的支持
perl -Mutf8 -CSAD -pe'...' filename
这里是 utf8 pragma如果源中有文字非 ASCII 字符,则需要,而其他标志可以在 command switches in perlrun 下看到
关于regex - 使用先行正则表达式替换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73995315/