7

I have a long list of something like:

AllowedUsers aaaa01@11.22.33.44 aaaa03@11.22.55.77 bbbb11@99.99.99.99 ccc00@11.0.0.11 .....

all in single line. Basically it's bunch of username@ip

I wanted to do a find and replace just by using its username (e.g. aaa) and replace it with nothing.

I tested my regex (RegExr) and it works as I expected. But when I try to do it on sed, it didn't work.

$ sed 's/bbbb11.*?(?= )//' test
AllowedUsers aaaa01@11.22.33.44 aaaa03@11.22.55.77 bbbb11@99.99.99.99 ccc00@11.0.0.11 .....

I also tried using bbbb11.*?(?=[[:space:]]), it didn't work either.

Did I miss something?

Inian
  • 12,807
annahri
  • 2,075

1 Answers1

12

The Positive (?=) and Negative lookahead (?!) assertions work well only in a tool that supports PCRE extensions. Neither GNU sed nor POSIX support these library extensions. You need perl which supports it out of the box

perl -pe 's/bbbb11.*?(?= )//'

Or you can very well achieve the same without the regex support. For such trivial substitutions you can do something as below which works in both POSIX and GNU sed

sed 's/\(bbbb11[^ ]*\)//'
Inian
  • 12,807