sort

sort linux command cheatsheet by Thamizhiniyan C S

Introduction

sort command, as the name suggests sorts the lines alphabetically and numerically, automatically.


Syntax

sort [OPTION]... [FILE]...


Important Flags

FlagsDescription

-r

Sorts in reverse order

-c

This flag is used to check whether the file is already sorted or not(If not, it will list, where the disorder started)

-u

To sort and removes duplicate lines(does work same as stdin redirected into uniq)

-o save.txt

To save into a output file

-t

Define Field Separator

-k

Reorders data in a specific field

-n

Sorts the file numerically

-M

Sorts by month names


Examples

CommandDescription
sort -d

Order the lines in lexicographical order

sort -r

Order the lines in reverse lexicographical order (i.e. Z-A instead of A-Z)

sort -n

Sort the lines in ascending order based on numbers

sort -n -r

Sort the lines in descending order based on numbers

sort -t $'\t' -k 2 -n -r

Sorts lines of text based on the second field (using a numerical comparison) in reverse order, considering tab characters as the field separator

sort -t $'\t' -k 2 -n

Sort lines of text based on the second field in ascending numerical order, considering tab characters as the field separator

sort -t '|' -k 2 -n -r

Sort lines of text based on the second field in descending numerical order, considering '|' as the field separator.

Last updated