Does lookbehind work in sed?

I created a test using grep , but it does not work in sed .

 grep -P '(?<=foo)bar' file.txt 

This works correctly by returning bar .

 sed 's/(?<=foo)bar/test/g' file.txt 

I was expecting a footest as a conclusion, but that did not work.

+5
source share
2 answers

Note that most of the time, you can avoid lookbehind (or lookahead) by using the capture group and backlink in the replacement string:

 sed 's/\(foo\)bar/\1test/g' file.txt 
+10
source

GNU sed does not support search statements. You can use a more powerful language such as Perl, or perhaps experiment with ssed , which supports Perl-style regular expressions.

 perl -pe 's/(?<=foo)bar/test/g' file.txt 
+9
source

Source: https://habr.com/ru/post/1203657/


All Articles