regexlinuxsedprependbackreference

Back-reference when preprend using sed linux command


I'm trying to prepend the first character of "monkey" using this command:

echo monkey | sed -E '/(.)onkey/i \1'

But when I use it like this, the output shows

1
monkey

I actually hope to see:

m
monkey

But back-reference doesn't work. Please someone tell me if it is possible to use Back-reference with \1. Thanks in advance.


Solution

  • You may use this sed:

    echo 'monkey' | sed -E 's/(.)onkey/\1\n&/'
    
    m
    monkey
    

    Here: