1. Home
  2. Docs
  3. Beginning UNIX on Raven
  4. Working with text files
  5. Sorting

Sorting

The sort command will sort the contents of a file (or redirected output from another command) using the entire line as the sort key.

head americanpie.txt | sort

Addition of the –r flag will reverse the sort order:

head americanpie.txt | sort –r

By default a the sort is done character by character along the line, which doesn’t always make the most sense when sorting numbers:

sort numbers.txt

10

11

9

The addition of the –n flag makes the program sort in numerical order:

sort –n numbers.txt

9

10

11