Can someone please explain to me why this code works (i.e. file2.txt is the alphabetically sorted contents of file1.txt):
cat file1.txt | sort > file2.txt
But when I do this, file1.txt blanks itself...?
cat file1.txt | sort > file1.txt
As you can guess, I'm trying to simply sort a file's contents alphabetically, then write back to the same file...
(I'm using mac 10.8.3's terminal. Date of writing is 19may2013)
The shell starts sort
after opening file1.txt
for output (truncating it, ie discarding all the data). Then it starts cat
with file1.txt
open for reading. The semantics of the shell are such that it might be feasible for the pipeline to get a page or so of input from file.txt
, but in practice almost all shells (which is to say all of them, but perhaps there are some shells I've never used that do not behave this way) will truncate the file before cat
ever reads any data.
To perform this operation, you must use a temporary file. (Well, it's not mandatory to use a temporary file. If the file is small enough, something like this will probably work cat file1.txt | ( sleep 2; sort > file1.txt )
, but is not guaranteed.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With