Sponge

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.

Last Updated: 2024-11-23

sponge is a non default command available via the moreutils package

It's key feature is that it waits for pipe to finish rather than streaming

echo "Put this text in a file" | sponge file1.txt


# Streaming equivalent with just built-ins
echo "Put this text in a file" > file1.txt

This features comes in handy when we want to read and write from a file in the same pipeline without data corruption

grep -v '^#' cities.txt > cities.txt
# grep: input file ‘cities.txt’ is also the output

Most people would get around with a tmp file

grep -v '^#' cities.txt > /tmp/cities.tmp
mv /tmp/cities.tmp ./cities.txt

But this is annoying

Enter sponge

grep -v '^#' cities.txt | sponge cities.txt

Resources