PDA

View Full Version : Question about sed



kevdog
January 9th, 2016, 05:19 PM
I'm not a sed expert but definitely not a beginner.

I've broken down my problem and I quite quite figure out why I can't match the expression #!/usr/bin/python. I'm trying to replace all occurrences of #!/usr/bin/python with #!/usr/bin/python2

Here is my simplified example:

sed -ri "s|^#!/usr/bin/python$|&2|g" setup.py

It returns:
bash: !/usr/bin/python$: event not found

It's not found because it's not matching the # symbol

If I use the expression:
sed -ri "s|#!/usr/bin/python$|&2|g" setup.py

Things work as expected, however I want to match the beginning of the line.
I'm confused what to do at this point.

steeldriver
January 9th, 2016, 05:26 PM
That's nothing to do with sed - it's because the shell is treating ! as a history expansion command

It should work if you change the double quotes to single quotes, I think

matt_symes
January 9th, 2016, 05:27 PM
Hi

Use single quotes


sed -ri 's|^#!/usr/bin/python$|&2|g' setup.py


matthew-xu-16-04:/home/matthew:0 % echo '#!/usr/bin/python' > test.py
matthew-xu-16-04:/home/matthew:0 % sed -ri 's|^#!/usr/bin/python$|&2|g' test.py
matthew-xu-16-04:/home/matthew:0 % cat test.py
#!/usr/bin/python2
matthew-xu-16-04:/home/matthew:0 %

I think that bash is performing command substitution in a similar way to this example.



matthew-xu-16-04:/home/matthew:0 % touch /test
touch: cannot touch ‘/test’: Permission denied
matthew-xu-16-04:/home/matthew:0 % sudo !!
sudo touch /test
[sudo] password for matthew:
matthew-xu-16-04:/home/matthew:0 %

EDIT:

Beaten to it :)

And you even had the correct parlance, steeldriver. History expansion.

Kind regards

steeldriver
January 9th, 2016, 05:28 PM
... but it's better with examples ;)