Software & Apps Linux Uses of the Linux Sort Command Items to be sorted must be delimited in some way by Juergen Haas Writer Former Lifewire writer Juergen Haas is a software developer, data scientist, and a fan of the Linux operating system. our editorial process Juergen Haas Updated on March 26, 2020 Tweet Share Email Linux Switching from Windows In This Article Basic Rules for Sorting Sort a Text File Reverse a Sort Save a Sorted File Sort a Stream Output Data in a Linux text file can be sorted with the sort command as long as each element is delimited in some way. Frequently, the comma is used as the separator for delimited information. David Lees / Iconica / Getty Images Basic Rules for Sorting The sort command rearranges the lines in a text file to sort the lines numerically and alphabetically. The default rules for the sort command are: Text that starts with a number appears before text that starts with a letter; lower numbers sort first. Text that starts with a letter that appears in the alphabet earlier than the first letter of other text entries is sorted before the text that starts with a letter that appears later in the alphabet. When text begins with uppercase and lowercase instances of the same letter, the lowercase instance is sorted first. Sort a Text File To sort the lines in a delimited Linux file, use the sort command like this: sort -k2 table1.txt This command sorts the file table1.txt according to the characters starting at the second column (k2 refers to the second column). Assuming the input file content is: 1, Justin, Timberlake, Title 545, Price $7.302, Taylor, Swift, Title 723, Price $7.903, Mick, Jagger, Title 610, Price $7.904, Lady, Gaga, Title 118, Price $7.305, Johnny, Cash, Title 482, Price $6.506, Elvis, Presley, Title 335, Price $7.307, John, Lennon, Title 271, Price $7.908, Michael, Jackson, Title 373, Price $5.50 Because the second column in this example contains first names, the sorted output is arranged by the first letter of the first name of each individual in the second column, as shown below: 6, Elvis, Presley, Title 335, Price $7.307, John, Lennon, Title 271, Price $7.905, Johnny, Cash, Title 482, Price $6.501, Justin, Timberlake, Title 545, Price $7.304, Lady, Gaga, Title 118, Price $7.308, Michael, Jackson, Title 373, Price $5.503, Mick, Jagger, Title 610, Price $7.902, Taylor, Swift, Title 723, Price $7.90 If you sort the file with -k3 (using the line contents starting at column 3, the Last Name column), the output is: 5, Johnny, Cash, Title 482, Price $6.504, Lady, Gaga, Title 118, Price $7.308, Michael, Jackson, Title 373, Price $5.503, Mick, Jagger, Title 610, Price $7.907, John, Lennon, Title 271, Price $7.906, Elvis, Presley, Title 335, Price $7.302, Taylor, Swift, Title 723, Price $7.901, Justin, Timberlake, Title 545, Price $7.30 and -k5 produces a list sorted by price: 4, Lady, Gaga, Title 118, Price $7.307, John, Lennon, Title 271, Price $7.906, Elvis, Presley, Title 335, Price $7.308, Michael, Jackson, Title 373, Price $5.505, Johnny, Cash, Title 482, Price $6.501, Justin, Timberlake, Title 545, Price $7.303, Mick, Jagger, Title 610, Price $7.902, Taylor, Swift, Title 723, Price $7.90 Wait, that didn't work, did it? Instead of sorting by price, the command sorted the list by the title number. Why? Well, unless it's given the delimiter at which it should break each line into columns, it uses spaces. Since there's a space in the middle of each title entry, column four in each entry becomes the word, "Title," and column five becomes the title number. Use the -t flag to specify the delimiter for sort. This controls exactly where the command separates out columns. sort -t, -k5 table1.txt This produces the desired result: 8, Michael, Jackson, Title 373, Price $5.505, Johnny, Cash, Title 482, Price $6.501, Justin, Timberlake, Title 545, Price $7.304, Lady, Gaga, Title 118, Price $7.306, Elvis, Presley, Title 335, Price $7.302, Taylor, Swift, Title 723, Price $7.903, Mick, Jagger, Title 610, Price $7.907, John, Lennon, Title 271, Price $7.90 Usually, you'll want to specify the delimiter. It prevents unwanted confusion and inaccurate results. This is especially true with larger files that might not always be obviously incorrect. Reverse a Sort The -r option reverses the sorting. For example, using the results above: sort -r -t, -k5 table1.txt yields: 7, John, Lennon, Title 271, Price $7.903, Mick, Jagger, Title 610, Price $7.902, Taylor, Swift, Title 723, Price $7.906, Elvis, Presley, Title 335, Price $7.304, Lady, Gaga, Title 118, Price $7.301, Justin, Timberlake, Title 545, Price $7.305, Johnny, Cash, Title 482, Price $6.508, Michael, Jackson, Title 373, Price $5.50 Save a Sorted File Sorting a file doesn't save it. To save the sorted list in a file, use the redirect operator: sort -k6 table1.txt > test_new.txt where test_new.txt is the new file. Sort a Stream Output You can also apply the sort command to the output of a stream, such as the pipe operator: ls -n | sort -t, -k5 This sorts the output of the file listing generated by the ls command by file size, starting with the largest files. The -n operator specifies numeric sorting rather than alphabetic. Was this page helpful? Thanks for letting us know! Get the Latest Tech News Delivered Every Day Email Address Sign up There was an error. Please try again. You're in! Thanks for signing up. There was an error. Please try again. Thank you for signing up. Tell us why! Other Not enough details Hard to understand Submit