On Tue, 30 Aug 2011 21:33:34 +0200, David Watzke<address@hidden> wrote:
Hi there,
I've encountered a strange bug in sed (or maybe it's not a bug and I'm
missing something here):
this command works fine and adds dashes between numbers:
sed -i -r "s/([0-9]+) ([0-9]+) ([0-9]+)$/\1-\2-\3/" mobily.txt
the file mobily.txt contains something like this (it's not important):
name surname 000 111 222
but the same command with -i and -r written in a short form like this
doesn't work:
sed -ir "s/([0-9]+) ([0-9]+) ([0-9]+)$/\1-\2-\3/" mobily.txt
sed: -e expression #1, char 39: invalid reference \3 on `s' command's RHS
Why is that?
It's not a bug. -i -r and -ir are NOT the same thing.
"-i -r" says "edit the file in place (pseudo), without making a backup (-i),
and use extended regular expressions (-r)".
"-ir" says "edit the file in place, and make a backup appending the
extension "r" to the original file name". So you may find a file
"mobily.txtr" in the same directory if the program ran successfully;
however, since you're not specifying -r anymore, the program fails because
it sees ERE syntax (in BRE, "(...)" are literal and do not create capture
groups).