bug-sed
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#36129: Error in sed manual


From: Assaf Gordon
Subject: bug#36129: Error in sed manual
Date: Fri, 7 Jun 2019 19:50:51 -0600
User-agent: Mutt/1.11.4 (2019-03-13)

tag 36094 notabug
close 36094
stop

Hello,

On Fri, Jun 07, 2019 at 07:10:12PM +0100, Gavin Rebeiro wrote:
> In section 3.3 (The s Command), we are told that both upper-case and
> lower-case `i' match provided regular expressions in a case-insensitive
> manner. However, in section 4.3 (selecting lines by text matching), we are
> told that lower-case `i' is reserved for the insert command.
> 
> The lower-case `i' needs to be removed from section 3.3.

This is conflating three different uses of "i".

Section 3.3 refers to flags to the 's' command, i.e. the letters
appearing after the third slash (like "g"):

     s/foo/bar/i
     s/foo/bar/I
     s/foo/bar/ig

In this case, both "i" and "I" affect the behaviour of the s/// command,
allowing case-insensitive regex match for the substitution (that is, "foo"
and "FOO" and "FoO" etc. will match and will be substituted with "bar").


Section 4.3 refers to flags to regular-expression addresses - a way to
select input lines based on content, before executing any command on
them. Here, only upper-case "I" makes the match case-insensitive:

    /foo/d
    /foo/Id

In the first example, lines containing lower-case 'foo' will be deleted
(due to regex matching, followed by the 'd' command).
In the second example, the upper-case 'I' tells sed to match
case-insensitively, meaning lines with "FOO" and "FoO" will also be
deleted. Because there is no "I" command, sed knows the "I" is part
of the address regex specification.

The comment in the manual says this:

    "In many other programming languages, a lower case i is used for
     case-insensitive regular expression matching. However, in sed the i is
     used for the insert command (see insert command)."

And what it means is that if we wrote the following command:

    /foo/id

sed will actaully interpret it as:
1. /foo/ - match lines with "foo"
2. "i" - the insert command (i.e. insert text before the matching line)
3. "d" - the text to insert.

Result:

    $ printf "%s\n" foo bar | sed '/foo/id'
    d
    foo
    bar

Therefore to allow case-insensitive address-matches, sed has to choose
a letter that isn't used for other commands - hence upper-case "I".

Hope this explains the issue.
I'm closing it as "not a bug", but discussion can continue by replying
to this thread.

regards,
 - assaf








reply via email to

[Prev in Thread] Current Thread [Next in Thread]