I've done this before, expect to mutate a file in place, something like $ tr '\t' ',' <file.txt >file.txt
and well, clearly the >
redirection truncates the file before <
can read it;
Asked
Active
Viewed 39 times
0

ThorSummoner
- 4,422
1 Answers
0
You cannot. The >
redirect operator will create a new empty file as part of the pipeline. Tools (like sed --in-place
) which appear to do so are creating a new file, writing to it, and moving the new file in place of the old one:
$ ls -i file; sed --in-place 's/foo/bar/' file; ls -i file
266110 file
262180 file

DopeGhoti
- 76,081
-
yeah, in effect you will always need some kind of journal if you want to mutate the file contents in place, and it just so happens that if the working set fits in memory, and you have no power failures interruptions, you can get away with using the source file in memory as the effective journal :shrug: atleast thats how I put words to it – ThorSummoner Jun 06 '18 at 20:57
I didn't see anything about how to operate on-place with < > or <> operators in that question, thanks for the link! Very helpful, but I did not find a useful or equivalent question/answer there to my specific question :bow:Specifically the answer https://unix.stackexchange.com/a/186126/61349 posted that there is basically no good way to do inplace mutations, either use a temporary file or a different file – ThorSummoner Jun 06 '18 at 20:46cut
change a file in place?, Modify a file without creating another file, Wait for stdout stream to finish and then add its contents to a file – ilkkachu Jun 06 '18 at 20:51you have to use a tool designed for in-place operations like
sed -i 's/\t/,/g' file.txt
– Tim Kennedy Jun 06 '18 at 20:55(read I < file.txt; tr 'e' 'i' <<<"""$I""" > file.txt)
, "what could possibly go wrong" – ThorSummoner Jun 06 '18 at 21:04