Sorting files with sort

If you want to be really lazy—er, um, smart—let UNIX sort files for you. You can use sort to, for example, sort your address book alphabetically—as opposed to the random order you might have entered addresses in (see Code Listing 6.17).

To sort files with sort:

  • sort address.book >
    → sorted.address.book
    To begin, type sort, followed by the name of the file you want to sort. UNIX will sort the lines in the file alphabetically and present the sorted results in the file you specify (here, sorted.address.book), as shown in Code Listing 6.17.

Tip

If you have multiple files to sort, you can use sort file1 file2 file3 > complete.sorted.file, and the output will contain the contents of all three files, sorted, of course.

Code ...

Get Unix: Visual QuickStart 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.