From d43f4311755646a90b9c26291956d04687ba3595 Mon Sep 17 00:00:00 2001 From: cjwatson <> Date: Mon, 6 Feb 2006 20:45:38 +0000 Subject: [PATCH] Unix tools: sponge --- 2006-02-06-sponge.txt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 2006-02-06-sponge.txt diff --git a/2006-02-06-sponge.txt b/2006-02-06-sponge.txt new file mode 100644 index 00000000..5898497a --- /dev/null +++ b/2006-02-06-sponge.txt @@ -0,0 +1,26 @@ +Unix tools: sponge + +

Joey +writes +about the lack of new tools that fit into the Unix philosophy. My favourite +of such things I've written is +sponge. 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 sed -i and perl -i 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.

+ +

I normally use sponge a bit like this:

+ +
+sed '...' file | grep '...' | sponge file
+
+ +

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.

-- 2.30.2