Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix: cat-ing a file out to itself - why does this blank the file? [duplicate]

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)

like image 494
Simon C Avatar asked Mar 24 '23 21:03

Simon C


1 Answers

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.)

like image 125
William Pursell Avatar answered Apr 06 '23 09:04

William Pursell