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 footest
as output, but it did not work.
I created a test using grep but it does not work in sed . This works correctly by returning bar . I was expecting footest as output, but it did not work. sed does not support lookaround assertions.
Note that SED doesn't have lookahead and therefore doesn't support the regex feature you were trying to use. It can be done in SED using other features, as others have mentioned.
The good news is that you can use lookbehind anywhere in the regex, not only at the start. If you want to find a word not ending with an “s”, you could use \b\w+(? <! s)\b.
find (both the GNU and BSD variants) do not support lookahead/lookbehind. GNU grep only supports the POSIX basic and extended regular expression variants as well as a couple of others.
GNU sed does not have support for lookaround assertions. You could use a more powerful language such as Perl or possibly experiment with ssed
which supports Perl-style regular expressions.
perl -pe 's/(?<=foo)bar/test/g' file.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With