4 ms·
A lot of these seem easily achieved in a POSIX shell manner, but most annoyingly to me > sponge: soak up standard input and write to a file I do that all the
by donatj 6y ago
A lot of these seem easily achieved in a POSIX shell manner, but most annoyingly to me
> sponge: soak up standard input and write to a file
I do that all the time...
cat > file.txt
Update: reading the comments on here, apparently sponge sucks up all content before opening the destination file which allows editing an input file in place. Minor advantage there.
- jpab 6y agoThe purpose of sponge is that you can do this: grep foo file.txt | sponge file.txt If you do this with redirections then file.txt will be truncated before it's been processed, leaving you with an empty file instead of what you wanted. Sponge collects its input first and then writes everything out at the end, so you can output to a file that was used as an input. (Parent updated while I was writing. Oh well)
- tarruda 6y agoThat doesn't seem like an efficient way to do it. Commands that process file in place (sed -i) write to a temporary file in the same filesystem and then rename to the target file, which works if you want to process files that don't fit into memory.
- mslm 6y agoWithout inspecting the implementation of `sponge` that's how I assumed it already internally worked.
- jpab 6y agoI apologise, I was speaking loosely. I don't know if sponge collects input in memory or in a file. (Frankly I've never needed to care since the files I've ever needed this tool for have all been small)
- deleted 6y ago[deleted]
- nextaccountic 6y agoNot minor; it's literally the point of this tool
- dwheeler 6y agoIt lets you edit in place, which is very handy. It also keeps the original file permissions, which "blah bar > foo ; mv foo bar" does not do. Convenient. Frankly, I think that should be in the posix standard.