Unix Commands  «Prev  Next»
Lesson 4 The sort command
Objective Use the sort command to sort by column.

Unix Sort Command

Use the UNIX sort command to sort the contents of a text file. The clothes file, listed below, contains an inventory of the clothes in my closet. Each line contains the color, description, and number for a particular type of clothing.
      
Black,coat,4
Blue,pair of shoes,5
Black,shirt,2
Green,shirt,3
Red,coat,1
Green,coat,1
Black,trousers,3                

How to use the Unix `sort` command to sort the contents of your text file

Here’s a clear description of how you can use the Unix `sort` command to sort the contents of your text file: # 📋 Goal You have a text file with comma-separated values like: Black,coat,4 Blue,pair of shoes,5 Black,shirt,2 Green,shirt,3 Red,coat,1 Green,coat,1 Black,trousers,3
You want to sort the entries using the `sort` command.
🛠️ Basic Sort by Entire Line # If you simply run:
sort filename.txt

(where `filename.txt` contains your data), the `sort` command sorts the lines alphabetically starting from the first character of each line.
🔹 In this case, it would sort by color name first (`Black`, `Blue`, `Green`, `Red`, etc.).
🛠️ Sorting by Specific Fields (Columns)
Since your fields are comma-separated, you can tell `sort`:
  • Use the comma as the field separator
  • Sort by a specific field number

You can do this with the `-t` and `-k` options:
Option Meaning
-t',' Set comma as the field separator.
-kN Sort based on field N (e.g., -k1 means "sort by first field").

✅ Examples
  1. Sort by First Field (Color)
    sort -t',' -k1 filename.txt
        
    • 🔹 This sorts alphabetically by color (Black, Blue, Green, Red).
  2. Sort by Second Field (Item)
    sort -t',' -k2 filename.txt
        
    • 🔹 This sorts by the item name (coat, pair of shoes, shirt, trousers).
  3. Sort by Third Field (Number)
    sort -t',' -k3,3n filename.txt
        
    • 🔹 This sorts numerically by the quantity (e.g., 1, 2, 3, 4, 5).
    • - -k3,3 ➔ sort by only the third field
    • - n ➔ numeric sort instead of alphabetical
📘 Summary Table for Sorting Example
Sort Objective Command
Sort alphabetically by Color (1st field) sort -t',' -k1 filename.txt
Sort alphabetically by Item (2nd field) sort -t',' -k2 filename.txt
Sort numerically by Quantity (3rd field) sort -t',' -k3,3n filename.txt

🔵 Extra Tip: Save the Sorted Output
If you want to save the sorted result into a new file:
sort -t',' -k1 filename.txt > sorted_file.txt

Or replace the original file safely (using a temp file):
sort -t',' -k1 filename.txt > temp.txt && mv temp.txt filename.txt

The file makes a great candidate for use with the sort command because it is a text file with a separate piece of information on each line. Each line contains three itemsseparated by commas. These items are interpreted as columns of information. The locations of the columns are identified with a number, starting with +0 for the first column (color), +1 for the second column (description), and +2 for the third column (quantity).
If I wanted to sort my clothes file by column, I would use the following command:
$ sort –t, +1 clothes

When forming a sort command, you should include the –t option which specifies the character used to separate columns in the file. The –t option must be followed by a separation character, such as the comma used in the above command. You must also indicate the column number and file name you want the sort command to act upon.

Sort does not modify your file

Just using the sort command does not permanently change the file you are sorting. It only shows what the file would look like if it were sorted using various options. If you want to change your file, use the –o option with the sort command. The following command changes the contents of the clothes file so that it is sorted by color, column +0.
$ sort –t, +0 –o clothes clothes

Using the –o option tells the sort command to store your output in a file, in this case clothes. This file can have the same name as your original file, which results in a new version of the original. If you use a different file name, then your new file will contain the sorted information and your original file will contain the unsorted information.

SEMrush Software 4 SEMrush Banner 4