user2836927
user2836927

Reputation: 37

sed replacement \1 not working

Typing the following on the command line:

    echo happy | sed -r s/\([p]\)\([p]\)/*\1*\2*/

I expect the following result:

    ha*p*p*y

Instead, this is the result:

    ha*1*2*y

I am using Ubuntu 12.04.3 LTS (GNU/Linux 3.2.0-53-generic x86_64) The shell is -ksh sed is 4.2.1 December 2010

The -r option allowed me to use \( and \). I thought it would also enable \1 and \2 but that doesn't seem to be the case. Is there another option I'm overlooking?

Upvotes: 0

Views: 4422

Answers (2)

Austin Phillips
Austin Phillips

Reputation: 15746

When typed on the command line, the shell is interpreting some of your backslash characters, so sed never sees them.

Instead, try one of these. Notice the single quotes which preserves the literal backslash characters.

echo happy | sed -r 's/([p])([p])/*\1*\2*/'

or

echo happy | sed 's/\([p]\)\([p]\)/*\1*\2*/'

Upvotes: 1

CS Pei
CS Pei

Reputation: 11047

you don't need -r just use

echo happy | sed  's/\(p\)\([p]\)/*\1*\2*/'

Upvotes: 1

Related Questions