The sort and uniq commands

The sort command reads its input and writes it out again with the lines sorted, by default, alphanumerically by the first column. Like most filter commands that came with Unix, the input can come from a pipe, from files, or from data typed at the terminal. The first two forms are the most useful:

$ sort /etc/shells
$ sort ~/file1 ~/file2
$ printf '%s\n' 'Line 2' 'Line1' | sort

sort can sort data according to a particular column of its input data. For example, to sort all the entries in /etc/passwd by home directory (column 6), we could write:

$ sort -t: -k6,6 /etc/passwd

The options used here are:

  • -t: Specifies the delimiter for the data that separates the columns, in this case a colon.
  • -k6,6: Specifies by which ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.