Today, my sed kung-foo seemed to be lacking, so I ended up having to split the sed command over a zillion lines…
Normally I’d do something like :
sed 's/foo/bar/g' tmp.txt > tmp2.txt sed 's/fo2/blah/g' tmp2.txt > tmp3.txt
But this obviously gets painful after a time, a different approach would be to use sponge where we can do :
sed 's/foo/bar/g' tmp.txt | sponge tmp.txt sed 's/fo2/blah/g' tmp.txt | sponge tmp.txt
Whereby ‘sponge’ soaks up standard input and when there’s no more, opens the output file. This gets around the obvious problem that :
sed 's/foo/bar/g' tmp.txt > tmp.txt
doesn’t work because the shell opens (and overwrites) tmp.txt before sed’s had a chance to do anything.
cat tmp.txt | sed s/foo/bar/g | sed s/fo2/blah/g | sponge tmp.txt I guess?
yes, probably.
And yes, I know I’m using unnecessary ‘cat’s
What’s wrong with sed -i?
-i ?
Ah – -i = edit files in place…
There’s also perl -p -i -e … but I can never remember that stuff.
Joe Wrigley
What’s wrong with sed -i?
It uses a temporary file that is created next to the file that you state after -i. It could be that you don’t have the rights to create that file. Sponge doesn’t use a temporary file (or maybe it does, i don’t know, but on another location where you have write rights for sure).