News for hackers. Stuff that matters.
Re:Moreutils – Unix tools that nobody thought to write (2012) by blauditore on Saturday August 22, 2020 @05:35AM (#24242945)
What is the difference between `sponge` and redirecting with `>` (or `>>`)? Is it about better compatibility with pipes or something?
Re:Moreutils – Unix tools that nobody thought to write (2012) by JoelMcCracken on Saturday August 22, 2020 @05:37AM (#24242957)
Well one thing is that if you’ve tried it you’ll run into the problem he illustrates there that you are writing to the Sam file you are reading from, which won’t work right.

My guess is sponge buffers all the input and then sends it to output once stdin is closed.

Re:Moreutils – Unix tools that nobody thought to write (2012) by circular_logic on Saturday August 22, 2020 @05:39AM (#24242965)
I wondered this too so looked at the man page [1]

> Unlike a shell redirect, sponge soaks up all its input before opening the output file. This allows constricting pipelines that read from and write to the same file.

[1] https://linux.die.net/man/1/sponge

Re:Moreutils – Unix tools that nobody thought to write (2012) by jleahy on Saturday August 22, 2020 @06:23AM (#24243135)
Imo sponge is the most useful of all of them. For example if you want to filter a file in place you can do ‘grep str file | sponge file’. If you do that with a redirect you’ll end up with an empty file.

Some commands (eg. sed) have an in-place option, but many don’t.