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.

6 Replies to “Sponge – Shell command”

  1. 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).

Leave a Reply

Your email address will not be published. Required fields are marked *