Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to `sed -i` on Solaris

On Linux sed -i will modify the input files in place. It doesn't work on Solaris, though.

sed -i '$ s/OLD/NEW/g' test        
sed: illegal option -- i

What can I use in place of sed -i on Solaris?

like image 918
lidia Avatar asked Aug 26 '10 14:08

lidia


2 Answers

It isn't exactly the same as sed -i, but i had a similar issue. You can do this using perl:

perl -pi -e 's/find/replace/g' file

doing the copy/move only works for single files. if you want to replace some text across every file in a directory and sub-directories, you need something which does it in place. you can do this with perl and find:

find . -exec perl -pi -e 's/find/replace/g' '{}' \;
like image 118
Charlie B Avatar answered Nov 12 '22 09:11

Charlie B


You'll need to replicate -i's behavior yourself by storing the results in a temp file and then replacing the original file with the temp file. This may seem inelegant but that's all sed -i is doing under the covers.

sed '$ s/OLD/NEW/g' test > test.tmp && cat test.tmp > test && rm test.tmp

If you care you could make it a bit more robust by using mktemp:

tmp=$(mktemp test.XXXXXX)
sed '$ s/OLD/NEW/g' test > "$tmp" && cat "$tmp" > test && rm "$tmp"
like image 23
John Kugelman Avatar answered Nov 12 '22 10:11

John Kugelman