Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Read and write same file using sponge

📅 2017-Feb-23 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ moreutils, sponge ⬩ 📚 Archive

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

© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘📧