The sort command in Unix and Linux is used to sort lines of text files. It can sort the lines alphabetically, numerically, or based on various other criteria. The sort command can also handle complex sorting tasks with various options to customize its behavior.
Basic Usage
The basic syntax for the sort command is:
sort [options] [file...]
file: The file(s) to be sorted. If no file is specified, sort reads from standard input.
Examples
Sorting a File Alphabetically
To sort the lines in a file alphabetically:
sortfile.txt
Sorting a File and Saving the Output
To sort a file and save the sorted output to another file:
sortfile.txt>sorted_file.txt
Options
Sorting in Reverse Order
To sort in reverse order, use the -r option:
sort-rfile.txt
Sorting Numerically
To sort numerically, use the -n option. This is useful when dealing with numbers:
sort-nnumbers.txt
Sorting by a Specific Field
To sort by a specific field (column), use the -k option followed by the field number. Fields are typically separated by spaces or tabs:
sort-k2file.txt
This command sorts file.txt by the second field.
Sorting by Multiple Fields
You can sort by multiple fields by specifying multiple -k options:
sort-k2-k3file.txt
This command sorts by the second field, and if there are ties, it sorts by the third field.
Ignoring Case
To perform a case-insensitive sort, use the -f option:
sort-ffile.txt
Removing Duplicates
To remove duplicate lines, use the -u option:
sort-ufile.txt
Advanced Options
Sorting with a Custom Delimiter
To sort based on a custom delimiter, use the -t option followed by the delimiter character. For example, to sort by a comma-separated value (CSV) file:
sort-t','-k2file.csv
Sorting by Human-Readable Numbers
If your file contains human-readable numbers (e.g., 1K, 2M), use the -h option to sort these correctly:
sort-hfile.txt
Sorting with Stable Sort
To ensure that the original order is preserved for equal elements, use the -s option:
sort-sfile.txt
Sorting by Month
If your file contains dates with month names and you want to sort them correctly, use the -M option:
sort-Mdates.txt
Practical Use Cases
Sorting a List of Names
To sort a list of names alphabetically:
sortnames.txt
Sorting Log Files by Date
Assuming your log file entries start with a date, you can sort them chronologically:
sort-k1,1-k2,2log.txt
Sorting Disk Usage
To sort the output of the du command (disk usage) by size:
du-h|sort-h
Combining with Other Commands
Sorting and Removing Duplicates
To sort a file and remove duplicate lines, you can combine sort with uniq:
sortfile.txt|uniq
Or use the -u option directly with sort:
sort-ufile.txt
Sorting Pipelined Input
You can pipe the output of other commands into sort:
ls-l|sort-k5-n
This command lists files in the current directory and sorts them by size (fifth column) numerically.
Summary
The sort command is a powerful and versatile tool for sorting lines of text in Unix and Linux. By understanding and using its various options, you can sort files alphabetically, numerically, and in many other ways. This command is essential for data processing, log analysis, and numerous other tasks.
help
Usage: sort [OPTION]... [FILE]...
or: sort [OPTION]... --files0-from=F
Write sorted concatenation of all FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
Ordering options:
-b, --ignore-leading-blanks ignore leading blanks
-d, --dictionary-order consider only blanks and alphanumeric characters
-f, --ignore-case fold lower case to upper case characters
-g, --general-numeric-sort compare according to general numerical value
-i, --ignore-nonprinting consider only printable characters
-M, --month-sort compare (unknown) < 'JAN' < ... < 'DEC'
-h, --human-numeric-sort compare human readable numbers (e.g., 2K 1G)
-n, --numeric-sort compare according to string numerical value
-R, --random-sort shuffle, but group identical keys. See shuf(1)
--random-source=FILE get random bytes from FILE
-r, --reverse reverse the result of comparisons
--sort=WORD sort according to WORD:
general-numeric -g, human-numeric -h, month -M,
numeric -n, random -R, version -V
-V, --version-sort natural sort of (version) numbers within text
Other options:
--batch-size=NMERGE merge at most NMERGE inputs at once;
for more use temp files
-c, --check, --check=diagnose-first check for sorted input; do not sort
-C, --check=quiet, --check=silent like -c, but do not report first bad line
--compress-program=PROG compress temporaries with PROG;
decompress them with PROG -d
--debug annotate the part of the line used to sort,
and warn about questionable usage to stderr
--files0-from=F read input from the files specified by
NUL-terminated names in file F;
If F is - then read names from standard input
-k, --key=KEYDEF sort via a key; KEYDEF gives location and type
-m, --merge merge already sorted files; do not sort
-o, --output=FILE write result to FILE instead of standard output
-s, --stable stabilize sort by disabling last-resort comparison
-S, --buffer-size=SIZE use SIZE for main memory buffer
-t, --field-separator=SEP use SEP instead of non-blank to blank transition
-T, --temporary-directory=DIR use DIR for temporaries, not $TMPDIR or /tmp;
multiple options specify multiple directories
--parallel=N change the number of sorts run concurrently to N
-u, --unique with -c, check for strict ordering;
without -c, output only the first of an equal run
-z, --zero-terminated line delimiter is NUL, not newline
--help display this help and exit
--version output version information and exit
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a
field number and C a character position in the field; both are origin 1, and
the stop position defaults to the line's end. If neither -t nor -b is in
effect, characters in a field are counted from the beginning of the preceding
whitespace. OPTS is one or more single-letter ordering options [bdfgiMhnRrV],
which override global ordering options for that key. If no key is given, use
the entire line as the key. Use --debug to diagnose incorrect key usage.
SIZE may be followed by the following multiplicative suffixes:
% 1% of memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y.
*** WARNING ***
The locale specified by the environment affects sort order.
Set LC_ALL=C to get the traditional sort order that uses
native byte values.
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report any translation bugs to <https://translationproject.org/team/>
Full documentation <https://www.gnu.org/software/coreutils/sort>
or available locally via: info '(coreutils) sort invocation'