Some alternative bash things…. ( avoiding unneecessary use of cat / awk / grep …. )
Sponge – Shell command
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.