Table of Contents

Home DTOS Knowledge Base Community Contribute Support DT’s Work


Sort lines of text files.

Sort a file in ascending order:

sort path/to/file

Sort a file in descending order:

sort -r path/to/file

Sort a file in case-insensitive way:

sort -f path/to/file

Ignore non-printable characters

sort -i path/to/file

Sort a file using numeric rather than alphabetic order:

sort -n path/to/file

Sort `/etc/passwd` by the 3rd field of each line numerically, using “:” as a field separator:

sort -t : -k 3n /etc/passwd

Sort a file preserving only unique lines:

sort -u path/to/file
cat /etc/shells | awk -F "/" '/^\// {print $NF}' | sort -u

Sort a file, printing the output to the specified output file (can be used to sort a file in-place):

sort path/to/file/read -o path/to/file/output

NOTE: the output file is the one immediately after the -o flag!

Sort randomly:

sort -R .bashrc

NOTE:. This is like a random permutation of the inputs (see shuf invocation), except that keys with the same value sort together.

Author: Derek Taylor

Created: 2021-08-27 Fri 15:34