sort
100%

Text-Fu · Lesson 12

sort

Learn how to order text lines by lexical, numeric, or selected field values with sort.

The sort command reads complete lines, orders them according to selected comparison rules, and writes the result to stdout. It does not change an input file unless you explicitly choose an output operation.

Sorting Complete Lines

Consider animals.txt:

dog
cow
cat
elephant
bird

Sort the lines in ascending order:

$ sort animals.txt
bird
cat
cow
dog
elephant

Text ordering follows the current locale, which can affect case, accents, and punctuation. Use a consistent locale such as LC_ALL=C when a script requires reproducible byte-oriented collation:

$ LC_ALL=C sort animals.txt

What does sort animals.txt do with no key or numeric option?

Reversing the Result

Add -r to reverse the comparison result:

$ sort -r animals.txt
elephant
dog
cow
cat
bird

Which command sorts animals.txt in reverse order?

Comparing Numbers

Lexical order compares characters, so 10 normally comes before 2. Use -n for ordinary numeric comparison:

$ printf '10\n2\n30\n' | sort -n
2
10
30

Combine options when needed. sort -nr scores.txt compares numerically and places larger values first.

Which command sorts numeric lines in scores.txt from largest to smallest?

Sorting by a Field

Use -k START[,END] to choose a key. Fields are separated by runs of blanks by default. For colon-separated records, use -t ':':

$ printf 'alice:30\nbob:8\ncarol:20\n' | sort -t ':' -k 2,2n
bob:8
carol:20
alice:30

Here, -t ':' selects the delimiter, -k 2,2 limits the key to field 2, and the attached n compares that key numerically. Without the ending ,2, a key starting at field 2 normally continues through the rest of the line.

Which command sorts users.txt numerically by only its second colon-separated field?

Removing Duplicates and Saving Output

Use -u to output one line for each equal comparison key:

$ sort -u names.txt

This both sorts and removes duplicates under the selected comparison rules. If you only want to remove adjacent duplicates from already sorted data, the uniq command covered later can do that.

To write the result to a file, ordinary redirection is fine when the destination differs from the input:

$ sort names.txt > names-sorted.txt

Do not run sort names.txt > names.txt; the shell truncates the input before sort reads it. GNU sort -o names.txt names.txt safely arranges its own output when you intentionally want the same pathname:

$ sort -o names.txt names.txt

Keep a backup or write and verify a separate result when the original data matters.

On GNU/Linux, which command asks sort to safely write the sorted result back to names.txt without shell redirection truncating it first?

To practice ordering and analyzing line-oriented data, try these hands-on labs:

  1. Linux sort Command: Text Sorting - This lab provides a direct introduction to the sort command, allowing you to practice sorting lines of text files in various ways, including ascending and descending order.
  2. Word Count and Sorting - In this challenge, you'll apply your knowledge of sorting along with word counting to analyze text data, helping you find frequent patterns and sort data efficiently.

Lesson complete

You finished sort

You can now choose comparison rules and destinations for sorted text.

  • Sort complete lines under an explicit locale when reproducibility matters.

  • Reverse results with -r.

  • Compare numeric values with -n.

  • Select a bounded field key with -t and -k.

  • Remove duplicates or save output without truncating the input.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Text-Fu