Chapter 23: Bash Sort Lines (sort)

What does sort actually do? (super simple first)

sort reads lines of text (from file or from pipe), sorts them alphabetically/numerically, and prints the sorted result.

By default:

  • Sorts lines (whole line)
  • Alphabetical order (A → Z, case-sensitive: uppercase before lowercase)
  • Ascending (small → big)

It does NOT change the original file – it shows sorted version on screen (redirect to save if you want).

Very useful for:

  • Sorting lists of names, IPs, numbers
  • Preparing data before uniq
  • Making logs/reports readable
  • Top-N lists (sort -rn | head)

1. Create test files right now (copy-paste these)

Bash

2. Basic usage (try these now!)

Bash

Notice:

  • Uppercase comes before lowercase (A before a)
  • Duplicates are kept
Bash
Bash

3. Most important options (you’ll use these 90% of time)

Option Meaning Example command Real use case
-f Fold lower/upper case (ignore case) sort -f names.txt Names, cities
-r Reverse (big → small) sort -r scores.txt Top scores first
-n Numeric sort (treat as numbers) sort -n numbers.txt Sizes, ports, scores
-k Sort by specific field/key sort -k 3 Sort by column
-t Field separator (delimiter) sort -t ‘,’ -k 5 CSV files
-u Unique (remove duplicates) sort -u names.txt Clean lists
-V Version sort (smart numbers) sort -V versions.txt 1.2.10 before 1.10
-b Ignore leading blanks sort -b Messy data
-R Random shuffle sort -R Randomize lines

4. Sorting by specific column / field (-k + -t)

This is where sort becomes super powerful

Bash

Multiple keys (sort by salary desc, then by name asc if salary same):

Bash

5. Real-world examples you will use daily

Bash

6. Quick cheat-sheet table

Goal Command example Notes
Alphabetical sort sort names.txt Default
Case-insensitive sort -f names.txt Most name lists
Numeric ascending sort -n numbers.txt Sizes, scores
Numeric descending (top first) sort -nr numbers.txt Leaderboards
Unique lines sort -u list.txt Clean duplicates
Sort by 2nd column (space sep) sort -k 2 file.txt Default tab/space
Sort CSV by 5th column numeric desc sort -t ‘,’ -k 5 -nr file.csv Salaries, prices
Top 10 most frequent words `… sort
Version-aware sort sort -V versions.txt Software versions
Stable sort (keep original order) sort -s When equal keys

7. Pro tips from daily use

  • Always use -n when sorting numbers (otherwise 100 comes before 20!)
  • Use -t + -k together for delimited files
  • sort | uniq -c | sort -nr = most common pattern for counting
  • sort -u removes duplicates but needs sorted input first
  • Combine with head/tail for top/bottom N
  • For huge files: sort is very efficient (uses temp files if needed)

Now open your terminal and try these 3 right now:

Bash

Tell me what you see! Or ask:

  • “How to sort by month name?”
  • “How to sort IP addresses correctly?”
  • “sort + uniq vs sort -u – difference?”

We’ll build exact commands together! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *