Subject: Re: bug#62144: Unexpected output

From: Jim Meyering <[email-masked]>
Date: Tue, 14 Mar 2023 09:24:28 -0700

tags 62144 notabug
stop

On Sun, Mar 12, 2023 at 7:24 AM walonen <[email-masked]> wrote:
> I have found an unexpected behavior of sed. I would expect the same
> output for the following commands:
>
> sed '/root/!d' /etc/passwd
> sed '/root/{ !d }' /etc/passwd

Thanks for the report, but the '!' modifier must be applied to an
address specification, so your latter command should be spelled like
this:

sed '/root/!{ d }' /etc/passwd

and that is equivalent to the first one.

Quoting the info documentation on this topic:

   Appending the '!' character to the end of an address specification
(before the command letter) negates the sense of the match.  That is, if
the '!' character follows an address or an address range, then only
lines which do _not_ match the addresses will be selected.  The
following command replaces the word 'hello' with 'world' only in lines
_not_ containing the word 'apple':

     sed '/apple/!s/hello/world/' input.txt > output.txt

   The following command replaces the word 'hello' with 'world' only in
lines 1 to 3 and 18 till the last line of the input file (i.e.
excluding lines 4 to 17):

     sed '4,17!s/hello/world/' input.txt > output.txt