Discussion:
sed bug
(too old to reply)
Peter Scott
2015-03-18 14:42:06 UTC
Permalink
Hi,

$ sed --version
sed (GNU sed) 4.2.2
Copyright (C) 2012 Free ...

# This works
#
$ sed 's/foo/bar/i'
FOO
bar
$

# I think this should work too.
#
$ sed '/foo/s//bar/i'
sed: -e expression #1, char 13: cannot specify modifiers on empty regexp
$

Thanks,
Peter Scott
--
email: ***@shu.ac.uk
website: http://peterscott.eu
NB: My mobile is a "not at home" phone; I don't hear or see it at home.
Jim Meyering
2015-03-18 19:37:25 UTC
Permalink
Post by Peter Scott
Hi,
$ sed --version
sed (GNU sed) 4.2.2
Copyright (C) 2012 Free ...
# This works
#
$ sed 's/foo/bar/i'
FOO
bar
$
# I think this should work too.
#
$ sed '/foo/s//bar/i'
sed: -e expression #1, char 13: cannot specify modifiers on empty regexp
Thanks for spotting and reporting that. I certainly think it should work.
I suspect it is simply an oversight, and we will fix it for the
upcoming release.
Bob Proulx
2015-03-18 20:39:04 UTC
Permalink
Post by Jim Meyering
Post by Peter Scott
# This works
#
$ sed 's/foo/bar/i'
FOO
bar
$
# I think this should work too.
#
$ sed '/foo/s//bar/i'
sed: -e expression #1, char 13: cannot specify modifiers on empty regexp
Thanks for spotting and reporting that. I certainly think it should work.
I suspect it is simply an oversight, and we will fix it for the
upcoming release.
Why should that work? The previous regular expression is repeated.
Therefore any case insensitive flags must be attached to the original
expression not the repeat of the original expression.

Isn't that a documented behavior?

The empty regular expression `//' repeats the last regular
expression match (the same holds if the empty regular expression is
passed to the `s' command). Note that modifiers to regular
expressions are evaluated when the regular expression is compiled,
thus it is invalid to specify them together with the empty regular
expression.

Plus 'i' isn't a traditional sed flag. It isn't in the legacy Unix
sed. GNU sed uses 'I' for that purpose, avoiding 'i' already used for
insert.

`/REGEXP/I'
`\%REGEXP%I'
The `I' modifier to regular-expression matching is a GNU extension
which causes the REGEXP to be matched in a case-insensitive manner.

Therefore the failure makes sense and is documented as such. The way
to do this action is this following way. Place the case-insensitive
flag for the regular expression to be matched in the pattern and then
it is applied when it is repeated using the empty expression
shorthand.

$ echo FOO | sed '/foo/Is//bar/'
bar

This allows separate control of the address range pattern and the
substitution pattern.

$ echo FOO | sed '/foo/Is/f/b/I'
bOO

Unfortunately the 'i' command is already used for inserting lines.
Therefore using 'i' often surprises people by yielding an insertion,
as it should do in retrospect, as a command instead of being a flag.

$ echo foo | sed '/foo/is/baz/bar/'
s/baz/bar/

The /foo/ matches and therefore the insert action is triggered.

`i\'
`TEXT'
As a GNU extension, this command accepts two addresses.

Immediately output the lines of text which follow this command
(each but the last ending with a `\', which are removed from the
output).

Since I think that is somewhat confusingly written here is the old sed
doc for that part. (Where (1) means 1 address max there.)

(1)i\
<text> -- insert lines
The i function behaves identically to the a function,
except that <text> is written to the output before the
matched line. All other comments about the a function
apply to the i function as well.

Bob
Jim Meyering
2015-03-18 21:31:45 UTC
Permalink
tags 20138 notabug
thanks
Post by Bob Proulx
Post by Jim Meyering
Post by Peter Scott
# This works
#
$ sed 's/foo/bar/i'
FOO
bar
$
# I think this should work too.
#
$ sed '/foo/s//bar/i'
sed: -e expression #1, char 13: cannot specify modifiers on empty regexp
Thanks for spotting and reporting that. I certainly think it should work.
I suspect it is simply an oversight, and we will fix it for the
upcoming release.
Why should that work? The previous regular expression is repeated.
Therefore any case insensitive flags must be attached to the original
expression not the repeat of the original expression.
Isn't that a documented behavior?
The empty regular expression `//' repeats the last regular
expression match (the same holds if the empty regular expression is
passed to the `s' command). Note that modifiers to regular
expressions are evaluated when the regular expression is compiled,
thus it is invalid to specify them together with the empty regular
expression.
Plus 'i' isn't a traditional sed flag. It isn't in the legacy Unix
sed. GNU sed uses 'I' for that purpose, avoiding 'i' already used for
insert.
`/REGEXP/I'
`\%REGEXP%I'
The `I' modifier to regular-expression matching is a GNU extension
which causes the REGEXP to be matched in a case-insensitive manner.
Therefore the failure makes sense and is documented as such. The way
to do this action is this following way. Place the case-insensitive
flag for the regular expression to be matched in the pattern and then
it is applied when it is repeated using the empty expression
shorthand.
$ echo FOO | sed '/foo/Is//bar/'
bar
This allows separate control of the address range pattern and the
substitution pattern.
$ echo FOO | sed '/foo/Is/f/b/I'
bOO
Unfortunately the 'i' command is already used for inserting lines.
Therefore using 'i' often surprises people by yielding an insertion,
as it should do in retrospect, as a command instead of being a flag.
$ echo foo | sed '/foo/is/baz/bar/'
s/baz/bar/
The /foo/ matches and therefore the insert action is triggered.
`i\'
`TEXT'
As a GNU extension, this command accepts two addresses.
Immediately output the lines of text which follow this command
(each but the last ending with a `\', which are removed from the
output).
Since I think that is somewhat confusingly written here is the old sed
doc for that part. (Where (1) means 1 address max there.)
(1)i\
<text> -- insert lines
The i function behaves identically to the a function,
except that <text> is written to the output before the
matched line. All other comments about the a function
apply to the i function as well.
Well, that will teach me to reply before looking at code/documentation.
Thanks, Bob! It is clearly not a bug after all.

Loading...