Quantcast
Viewing all articles
Browse latest Browse all 16

Answer by kine for sed in-place flag that works both on Mac (BSD) and Linux

If you really want to just use sed -i the 'easy' way, the following DOES work on both GNU and BSD/Mac sed:

sed -i.bak 's/foo/bar/' filename

Note the lack of space and the dot.

Proof:

# GNU sed% sed --version | head -1GNU sed version 4.2.1% echo 'foo'> file% sed -i.bak 's/foo/bar/' ./file% lsfile  file.bak% cat ./filebar# BSD sed% sed --version 2>&1 | head -1sed: illegal option -- -% echo 'foo'> file% sed -i.bak 's/foo/bar/' ./file% lsfile  file.bak% cat ./filebar

Obviously you could then just delete the .bak files.


Viewing all articles
Browse latest Browse all 16

Trending Articles