Ubuntu – Unique sorting: Redirect output to the same file

bashcommand line

Is there any short way to save the pipe output to the same file that it's processed. For example, this is what I am actually doing

$ cat filename | sort | uniq > result
$ rm -f filename
$ mv result filename

I was wondering if there was a way to do it in just one line (not appending those commands using &&)

This is not the way, but to get an idea

$ cat filename | sort | uniq > filename

Best Answer

You can use sponge from moreutils package:

LC_ALL=C sort -u filename | sponge filename

You also don't need pipe to uniq, since when sort has -u option to unique lines when sorting.

Note that on GNU system with UTF-8 locales, sort -u or sort | uniq didn't give you unique lines, but the first from sequence of lines which sort the same in current locale.

$ printf '%b\n' '\U2460' '\U2461' | LC_ALL=en_US.utf8 sort | LC_ALL=en_US.utf8 uniq

gave you only . Changing locale to C force the sorting order based on the byte values:

$ export LC_ALL=C
$ printf '%b\n' '\U2460' '\U2461' | LC_ALL=C sort | LC_ALL=C uniq
Related Question