在sed中看后面的工作? [英] Does lookbehind work in sed?
本文介绍了在sed中看后面的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 grep
创建了一个测试,但它在 sed
中不起作用。
I created a test using grep
but it does not work in sed
.
grep -P '(?<=foo)bar' file.txt
这可以通过返回 bar
来正常工作。
This works correctly by returning bar
.
sed 's/(?<=foo)bar/test/g' file.txt
我期待 footest
作为输出,但它不起作用。
I was expecting footest
as output, but it did not work.
推荐答案
请注意,大多数情况下,您可以使用替换字符串中的捕获组和反向引用来避免向后看(或向前看):
Note that most of the time you can avoid a lookbehind (or a lookahead) using a capture group and a backreference in the replacement string:
sed 's/\(foo\)bar/\1test/g' file.txt
这篇关于在sed中看后面的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文