4 ms·
> sponge: soak up standard input and write to a file How does this differ than just redirecting to a file in the shell?
by bluejekyll 10y ago
> sponge: soak up standard input and write to a file
How does this differ than just redirecting to a file in the shell?
- cgag 10y agoIf you do something like: echo "hello world" >> dummyfile cat dummyfile | sed 's/hello/goodbye/' > dummyfile dummyfile will be blank afterwards if you do: cat dummyfile | sed 's/hello/goodbye/' | sponge dummyfile It'll work as you'd expect (or at least as I'd expect). Otherwise `> dummyfile` will truncate dummyfile before it's read and inputed into sed.
- bluejekyll 10y agoAh, yes. I like it. I've definitely made the exact mistake you point out. Thanks!
- cgag 10y agoI actually think I learned about sponge after I burned myself with this same mistake.
- StillBored 10y agoFor the sed example, what you actually want is -i which modifies the given file. AKA sed -i 's/hello/goodby/' dummyfile
- phamilton 10y agoThat's only in gnu coreutils. BSD coreutils don't support -i
- cheiVia0 10y agodidn't https://www.freebsd.org/cgi/man.cgi?query=sed https://www.freebsd.org/cgi/man.cgi?query=sed http://man.openbsd.org/OpenBSD-current/man1/sed.1 http://man.openbsd.org/OpenBSD-current/man1/sed.1 http://netbsd.gw.com/cgi-bin/man-cgi?sed++NetBSD-current http://netbsd.gw.com/cgi-bin/man-cgi?sed++NetBSD-current
- deleted 10y ago[deleted]
- mfontani 10y agoIsn't that what `tee` does, minus the "also displaying on STDOUT" bit?
- FreeFull 10y agotee doesn't store all the input before writing as far as I know, so it wouldn't work either in this case.
- Chris2048 10y agoIs this true? I though the issue was that the write might happen while the file is still being read? Does the '>' operator blanks the file first, before the read happens?
- ferrari8608 10y ago> erases the file first and >> appends without erasing This isn't the issue.
- parenthephobia 10y agosponge only writes the file once all input has been read, and - if the file already exists - tries to do the operation atomically by writing to a temporary file and renaming it over the old file.