0

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;

How do I mutate a file inplace without truncating/deleting it?

ThorSummoner
  • 4,422

1 Answers1

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