Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed command not working in mac

Tags:

bash

sed

Following sed command is not working on my lion mac.

find . -type f -exec sed -i 's/user_dashboard/user/g' {} \;

I am getting this error

sed: 1: "./vendor/assets/javascr ...": invalid command code .
like image 998
Nick Vanderbilt Avatar asked Mar 16 '12 20:03

Nick Vanderbilt


3 Answers

The OSX version of sed is not the same as those found in most Linux systems.

It extends the -i option to give you the opportunity save a file with a different extension, but requires input for that extension.

If you just want to overwrite the file in place, you need to use sed -i "" ...sedCmd.... fileName to rename your file in-place.

Per @JamesMcMahon 's comment, see here for the full doc for OSX/BSD sed.

I hope this helps.

like image 55
shellter Avatar answered Oct 20 '22 19:10

shellter


-i probably has a different meaning (not "in-place") in your version of sed. Try using gsed if available or replacing -i with -e and using a temporary file (and a mv afterwards) to emulate it.

like image 28
Eduardo Ivanec Avatar answered Oct 20 '22 18:10

Eduardo Ivanec


replacing text inside a file on the fly with sed on mac is possible,

the command is just a little different.

with: -i , you have to specify a postfix, which sed will use to save the original file after it modified it.

run the command as:

$ sed -i _backup -E 's/foo/bar/' /tmp/jestinkt.txt

ending with both the modified /tmp/jestinkt.txt and the original /tmp/jestinkt.txt_backup

like image 27
zenin Avatar answered Oct 20 '22 18:10

zenin