chiark / gitweb /
Unix tools: sponge
authorcjwatson <>
Mon, 6 Feb 2006 20:45:38 +0000 (20:45 +0000)
committercjwatson <>
Mon, 6 Feb 2006 20:45:38 +0000 (20:45 +0000)
2006-02-06-sponge.txt [new file with mode: 0644]

diff --git a/2006-02-06-sponge.txt b/2006-02-06-sponge.txt
new file mode 100644 (file)
index 0000000..5898497
--- /dev/null
@@ -0,0 +1,26 @@
+Unix tools: sponge
+
+<p>Joey
+<a href="http://kitenet.net/~joey/blog/entry/unix_tools_vidir-2006-02-05-21-33.html">writes</a>
+about the lack of new tools that fit into the Unix philosophy. My favourite
+of such things I've written is
+<a href="http://riva.ucam.org/svn/cjwatson/bin/sponge">sponge</a>. It
+addresses the problem of editing files in-place with Unix tools, namely that
+if you just redirect output to the file you're trying to edit then the
+redirection takes effect (clobbering the contents of the file) before the
+first command in the pipeline gets round to reading from the file. Switches
+like <kbd>sed -i</kbd> and <kbd>perl -i</kbd> work around this, but not
+every command you might want to use in a pipeline has such an option, and
+you can't use that approach with multiple-command pipelines anyway.</p>
+
+<p>I normally use sponge a bit like this:</p>
+
+<pre>
+sed '...' file | grep '...' | sponge file
+</pre>
+
+<p>Since it's so trivial I imagine lots of other people have written
+something similar (another common name for it seems to be inplace; my name
+indicates soaking up all the input and then squeezing it all out again); but
+I do keep meaning to try to get a rewritten version into coreutils at some
+point.</p>