Discussion:
sed numbered fields don't start from parenthesis
(too old to reply)
blind Pete
2014-10-02 12:41:44 UTC
Permalink
Hi,

I believe that the output from the following should be "Sep", not
"28th_Sep". The result can be changed by adding a ".*" to the
start of the search string, but I don't think that that should be
necessary. Removing the trailing ".*" also produces odd results.

Using Ubuntu 12.04 LTS.

$ echo 28th_September_2014 | sed "s/\(sep\).*/\1/i"
28th_Sep

$ sed --version
GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE, to the extent permitted by law.

GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-gnu-***@gnu.org>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
$
--
testing
bP
Andreas Schwab
2014-10-02 12:52:38 UTC
Permalink
Post by blind Pete
$ echo 28th_September_2014 | sed "s/\(sep\).*/\1/i"
28th_Sep
The pattern matches "September_2014", which is replaced with "Sep".
Everything else remains untouched.

Andreas.
--
Andreas Schwab, ***@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
Eric Blake
2014-10-02 12:53:21 UTC
Permalink
Post by blind Pete
Hi,
I believe that the output from the following should be "Sep", not
"28th_Sep". The result can be changed by adding a ".*" to the
start of the search string, but I don't think that that should be
necessary. Removing the trailing ".*" also produces odd results.
Sorry, but the behavior you see is mandated by POSIX, and not a bug.
Post by blind Pete
Using Ubuntu 12.04 LTS.
$ echo 28th_September_2014 | sed "s/\(sep\).*/\1/i"
The s/// command has two steps: first, find the matching portion of the
entire line:

28th_September_2014
^^^^^^^^^^^^^^

then replace the portion of the line that matched with the substitution
string:

28th_Sep

You _do_ have to use .* on both front and end of the pattern if you want
to shorten an entire line down to just a matched backref number.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
blind Pete
2014-11-02 00:32:52 UTC
Permalink
On Thu, 02 Oct 2014 06:53:21 -0600
Post by Eric Blake
Post by blind Pete
Hi,
I believe that the output from the following should be "Sep", not
"28th_Sep". The result can be changed by adding a ".*" to the
start of the search string, but I don't think that that should be
necessary. Removing the trailing ".*" also produces odd results.
Sorry, but the behavior you see is mandated by POSIX, and not a bug.
I was completely wrong.

Could the man pages me made more obvious by adding a line like,
"Note: only matched sections are processed, others are passed though
unchanged.".

If you get a lot of misguided bug reports, that could reduce the
noise.

Sorry for the inconvenience. Thank you for the good work.
Post by Eric Blake
Post by blind Pete
Using Ubuntu 12.04 LTS.
$ echo 28th_September_2014 | sed "s/\(sep\).*/\1/i"
The s/// command has two steps: first, find the matching portion of
28th_September_2014
^^^^^^^^^^^^^^
then replace the portion of the line that matched with the
28th_Sep
You _do_ have to use .* on both front and end of the pattern if you
want to shorten an entire line down to just a matched backref number.
--
testing
bP
Eric Blake
2014-11-05 16:02:08 UTC
Permalink
Post by blind Pete
Post by Eric Blake
Sorry, but the behavior you see is mandated by POSIX, and not a bug.
I was completely wrong.
Could the man pages me made more obvious by adding a line like,
"Note: only matched sections are processed, others are passed though
unchanged.".
Where in the man page are you proposing adding it? But I already see
this in 'man sed':


s/regexp/replacement/
Attempt to match regexp against the pattern space. If
success‐
ful, replace that portion matched with replacement.

where the "replace that portion matched" implies that the portion not
matched is preserved unchanged.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Loading...