5

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?

1
  • 2
    Your expression appears to be a Perl-compatible regular expression (PCRE). The sed utility expects a POSIX basic regular expression, or a POSIX extended regular expression if you use its non-standard -E option. This question appears to be a duplicate of Why does my regular expression work in X but not in Y?
    – Kusalananda
    Feb 4, 2020 at 6:07

1 Answer 1

8

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[^ ]*\)//'
1
  • Thanks for adding regex alternative for sed.
    – annahri
    Feb 4, 2020 at 7:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.