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.


Posted

in

by

Tags:

Comments

6 responses to “Sponge – Shell command”

  1. Duncan Avatar
    Duncan

    cat tmp.txt | sed s/foo/bar/g | sed s/fo2/blah/g | sponge tmp.txt I guess?

  2. David Goodwin Avatar

    yes, probably.
    And yes, I know I’m using unnecessary ‘cat’s

  3. Joe Wrigley Avatar

    What’s wrong with sed -i?

  4. David Goodwin Avatar

    Ah – -i = edit files in place…

    There’s also perl -p -i -e … but I can never remember that stuff.

  5. anon Avatar
    anon

    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 *