Timeline for Is there a command that makes another command "in-place"?
Current License: CC BY-SA 4.0
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Oct 11, 2023 at 5:52 | comment | added | ilkkachu | @StéphaneChazelas, yes, indeed | |
| Oct 11, 2023 at 5:47 | comment | added | Stéphane Chazelas |
@ilkkachu, rather inplace() { "${@:2}" < "$1" | sponge -- "$1"; } to deal with arbitrary file paths. Or inplace() (file="${1?}"; shift; <"$file" "$@" | sponge -- "$file") to avoid that ksh93ism. Setting the pipefail option in there would also be useful to report failure of the command (in any case, the file is lost if the command fails).
|
|
| Oct 10, 2023 at 23:14 | comment | added | Chris Davies | Related, first solution in this answer | |
| Oct 10, 2023 at 21:38 | history | edited | Lorenz Leutgeb | CC BY-SA 4.0 |
Mentioned that I know about `sed -i` and improved my definition according to @ilkkachu's comment.
|
| Oct 10, 2023 at 20:56 | comment | added | ilkkachu |
Anyway, quote the expansions so that your function works with arbitrary filenames: inplace() { "${@:2}" < "$1" | sponge "$1" }
|
|
| Oct 10, 2023 at 20:54 | comment | added | ilkkachu |
In a way, it's odd that sponge itself doesn't provide the wrapper functionality... But it's also impossible to make a really generic wrapper, because an arbitrary tool might edit the given file in-place itself, and not print to stdout. Using the wrapper with one like that might be problematic. Also there's the question of caching the data to memory vs. to a temporary file, and overwriting the target in-place vs. creating a new file with the same name... (not sure what sponge does)
|
|
| Oct 10, 2023 at 20:54 | answer | added | raj | timeline score: 0 | |
| S Oct 10, 2023 at 20:42 | review | First questions | |||
| Oct 24, 2023 at 20:42 | |||||
| S Oct 10, 2023 at 20:42 | history | asked | Lorenz Leutgeb | CC BY-SA 4.0 |