Does sort support sorting a file in-place, like `sed –in-place`?

Am I blind or is there no option like --in-place for sort?

In order to save results to the input file, sed uses -i (--in-place).

Redirecting the output of sort to the input file

sort < f > f

results in making it empty. If there is no --in-place option – maybe there is some trick how to do this in handy way?

(The only thing that cames to my mind:

sort < f > /tmp/f$$ ; cat /tmp/f$$ > f ; rm /tmp/f$$

Moving is not right choice, cause file permissions might be changed. That’s why I overwrite with the contents of the temp file which I then remove.)

sort has the -o (or --output) option that takes a filename as argument. 
The program writes the data to a temporary file,
then overwrites the original input file after the sort is complete
(which can happen only after all the input data have been read). 
(This is essentially the same thing as what sed -i does.)

From GNU sort info page:


      Write output to OUTPUT-FILE instead of standard output. Normally,
      sort reads all input before opening OUTPUT-FILE, so you can
      safely sort a file in place by using commands like sort -o F F
      and cat F | sort -o F. However, sort with --merge (-m)
      can open the output file before reading all input, so a command
      like cat F | sort -m -o F - G is not safe, as sort might start
      writing F before cat is done reading it.

      On newer systems, -o cannot appear after an input file if
      POSIXLY_CORRECT is set, e.g., sort F -o F. Portable scripts
      should specify -o OUTPUT-FILE
      before any input files.

and from The Open Group Base Specifications Issue 7:

    -o output

      Specify the name of an output file to be used instead of the standard
      output. This file can be the same as one of the input files.

There have been reports that sort might discard (i.e., destroy)
some or all of your data
if you are out of disk space or out of disk quota,
or the system crashes while sort is writing the output file,
or some other error occurs.

In short, to sort a file in place, the following may be used:

sort -o filename filename
Answered By: enzotib

It’s dangerous to overwrite the input file with the output file, because if the program or the system crashes while the file is being written, you’ve lost both.

A few programs (mostly GNU versions) have an in-place option (e.g. -i on perl and GNU sed; -o on GNU sort). They work by putting the data in a temporary file and then moving it into place.

For programs that have no such option, Colin Watson’s sponge utility (included in Joey Hess’s moreutils) does the job atomically for any program (examples: Can I make cut change a file in place?; How can I make iconv replace the input file with the converted output?). However, beware that do_stuff | sponge myfile always overwrites myfile, possibly with empty or incorrect output, even if do_stuff fails: sponge can’t detect whether the command that’s piped into it succeeded. So it’s handy to type, but it’s unsafe, compared to using a temporary file.

Only in those rare cases where you can’t recreate the original file with the same permissions do I recommend overwriting the file in place. In this case, you’d better save the original input somewhere. And then you can simply process the copy of the input and send it into the original file.

cp -p f ~/f.backup
sort <~/f.backup >|f
rm ~/f.backup # optional

Use -o or try the vim-way:

$ ex -s +'%!sort' -cxa file.txt
Answered By: kenorb

You can use the sponge function, that first soaks the stdin and then writes it to a file, like:

sort < f | sponge f

The downside of sponge is that it will store the output temporary in memory, which can be problematic for large files. Otherwise you have to write it to a file first and then overwrite the original file.

As however is pointed out by other answers, in place modifications are in general not a good idea, since in the middle of a process (for instance the sponge one), the machine might crash and then you can lose both the original and new file. You better first write it to a different file and then use an atomic mv (move) instruction.

Answered By: Willem Van Onsem
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.