I have the following sed command which puts brackets around the first word – (hello) – i thought if I wanted to change this to put the brackets around the second word all I had to do was change (\1) to (\2) as I thought this would pick up the second word but it does not work – what is the correct way to reference rthe second word then ?
echo "hello world" | sed -re 's/(^\w*)/(\1)/'
The \2 does not refer to the second word, but to the second bracketed expression. Therefore, bracketing both words should work: