njk2015
njk2015

Reputation: 543

cannot reference capture group in sed

I want to append a character on to a string.

I have this:

sed -r "s/\(.+:.+\)/\1,f/" "123:abc"

I simply want to append a ,f to the end of the string and am trying to reference the capture group \(.+:.+\). But, it does not work. I keep getting this error when I try to reference the capture group \1:

sed: -e expression #1, char 17: invalid reference \1 on `s' command's RHS

And idea?

Upvotes: 0

Views: 68

Answers (1)

John Watson
John Watson

Reputation: 129

You're using POSIX Basic syntax (with escaped parenthesis) when you specified the -r flag, which signifies POSIX Extended syntax.

More on this subject

Don't escape the parenthesis, and this should work. Sed is complaining because it doesn't think there is a group to reference, but instead, that there are literal parenthesis to find.

... "s/(.+:.+)/\1,f/" ...

i.e.

>echo "123:abc" | sed -r "s/(.+:.+)/\1,f/"
123:abc,f

Upvotes: 1

Related Questions