Unique sorting: Redirect output to the same file
Solution 1:
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
①
②
Solution 2:
You don't need any extra command like cat
and uniq
and also without using rm
command and mv
command to removing and renaming the filename. just use simple command.
sort -u filename -o filename
-u, --unique
with -c, check for strict ordering; without -c, output only the
first of an equal run
-o, --output=FILE
write result to FILE instead of standard output
How it work?
sort
command sorts your filename and with -u
option, removes duplicate lines from it. then with -o
option writes the output to the same file with in place method.