Linux sort Command
Learn Linux sort Command with example
Published
- Linux sort Command
- Set up fruits.txt file to Understand sort Command
- sort Command Example
- sort Command Output Sorted Content another File
- sort -r Command Sort in Reverse Order
- sort -nk2 Command Sort by 2nd Column
- sort -nk7 Command Sort by 7th Column
- sort -k Command Sort by 9th Non-numeric Column
- sort -u Command Remove Duplicate Content
- sort -f Command Case Incensitive Sort
- sort Multiple files
- sort Command Pipe with another Command
- Sort Based on More than One Field
- sort Command in Linux (Documentation)
Linux sort Command
In Linux we can use “sort” command to sort lines of text files.
Sort command takes blank space as field separator and entire Input file as sort key.
Note: sort command doesn’t sort the source files but only print the sorted output to terminal, until your redirect the output to new file.
sort command Syntax
>> sort [OPTION]... [FILE]...
Set up fruits.txt file to Understand sort Command
Let’s create a file first that we will use to understand sort Command.
Create a file fruits.txt using echo command.
>> echo -e "Kiwi\nMango\nDate\nApple\nCherry\nBanana\nAppricot\nWaterMelon"
sort Command Example
Now, use sort Command to sort the fruits.txt file content.
~$ sort fruits.txt
By default, sort command just output the content into the terminal not in the actual file.
Sort the contents of the file ‘fruits.txt’ and write it to a file called (fruits_sorted.txt). Also, verify the new sorted file content by using cat command.
sort Command Output Sorted Content another File
~$ sort fruits.txt > fruits_sorted.txt
~$ cat fruits_sorted.txt
sort -r Command Sort in Reverse Order
~$ sort -r fruits.txt
sort -nk2 Command Sort by 2nd Column
Let’s create a new file by using ls -l and output the content to list1.txt
~$ ls -l > list1.txt
~$ cat list1.txt
~$ sort -nk2 lsl.txt
sort -nk7 Command Sort by 7th Column
~$ sort -nk7 lsl.txt
sort -k Command Sort by 9th Non-numeric Column
~$ sort -k6 list1.txt
sort -u Command Remove Duplicate Content
~$ sort -u fruits.txt
sort -f Command Case Incensitive Sort
~$ sort -f fruits.txt
sort Multiple files
Let create one more file list2.txt and put outout of ls -al to learn sort multiple files.
~$ ls -al > list2.txt
~$ cat list2.txt
~$ sort list1.txt list2.txt
~$ sort -u list1.txt list2.txt
~$ sort -ur list1.txt list2.txt
sort Command Pipe with another Command
~$ ls -l /home/$USER | sort -k9
Sort Based on More than One Field
~$ $ ls -l /home/$USER | sort -t "," -nk7 -k6
sort Command in Linux (Documentation)
~$ man sort
NAME
sort - sort lines of text files
SYNOPSIS
sort [OPTION]... [FILE]...
sort [OPTION]... --files0-from=F
DESCRIPTION
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'
a
-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