Read and write same file using sponge

A common operation I end up doing is reading a text file, processing it using Unix tools and writing back the result to the same filename. However, if you do this you will find that you end up with an empty file! This is because the file is first opened for writing, thus clearing its contents.

sponge is a tiny tool created specially for this common operation. It can be installed easily:

$ sudo apt install moreutils

Here is an example that is wrong and ends up creating an empty file:

$ cat foobar.txt | uniq > foobar.txt

To fix this, add sponge to soak up all the output first and only write to the file at the end:

$ cat foobar.txt | uniq | sponge foobar.txt

Tried with: moreutils 0.57 and Ubuntu 16.04

Advertisements

2 thoughts on “Read and write same file using sponge”

  1. Thanks for helping me find this tool!

    There’s a small bug in your example though, it should be:

    $ cat foobar.txt | uniq | sponge foobar.txt

    If you redirect from sponge to foobar.txt, you end up with the original unwanted behaviour

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s