Home > Software engineering >  replace text using lookahead regex
replace text using lookahead regex

Time:10-08

I am trying to modify a few lines of a text file.

# cat example.txt
tested
tests
testing

If the word 'test' is not followed by 'ed' or 's' then change it to word 'work'. The following exmpression is working as expected:

test(?!ed|s)

But it does not work in sed as like this...

# sed -r 's/test\(?!ed\|s\)/work/g' example.txt
tested
tests
testing

The expected output is:

tested
tests
working

I guess sed does not support lookahead or lookback. Is there any other easy linux command for this?

CodePudding user response:

perl -pe's/test(?!ed|s)/work/g' filename

For Unicode (non-ASCII) text we need to enable support for it

perl -Mutf8 -CSAD -pe'...' filename

Here the utf8 pragma is needed if there are literal non-ASCII characters in the source, while the other flags can be seen under command switches in perlrun

CodePudding user response:

This might work for you (GNU sed):

sed -E 's/\<(test)(ed|s)?\>/\1\n\2/g;s/\<test(\S )/work\1/g;s/\n//g' file

If a word optionally ends in a defined ending, insert a newline between the word and its allowed ending.

If the same word does not end in a newline, replace the word by work.

Remove all scaffolding newlines.

  • Related