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)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# Simple names cat > names.txt << 'EOF' priya Rahul arman Suresh Anjali priya Rahul EOF # Numbers mixed cat > scores.txt << 'EOF' Math 85 Arman Science 92 Rahul English 78 Priya Physics 100 Suresh Chemistry 65 Anjali Math 92 Arman EOF # CSV-like with salaries cat > employees.txt << 'EOF' 101,Arman,Hyderabad,Developer,85000 102,Rahul,Delhi,Manager,120000 103,Priya,Bangalore,Tester,65000 104,Suresh,Hyderabad,Developer,90000 105,Anjali,Chennai,HR,70000 EOF |
2. Basic usage (try these now!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Sort names alphabetically (default) sort names.txt # Output: # Anjali # Rahul # Rahul # Suresh # arman # priya # priya |
Notice:
- Uppercase comes before lowercase (A before a)
- Duplicates are kept
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Case-insensitive sort (-f) sort -f names.txt # Anjali # arman # Priya # priya # Rahul # rahul # Suresh |
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Reverse order (descending) -r sort -r names.txt # Suresh # Rahul # Rahul # priya # priya # arman # Anjali |
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
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Sort employees by name (column 2, comma separated) sort -t ',' -k 2 employees.txt # 105,Anjali,Chennai,HR,70000 # 101,Arman,Hyderabad,Developer,85000 # ... # Sort by salary (column 5) numerically descending sort -t ',' -k 5 -n -r employees.txt # 102,Rahul,Delhi,Manager,120000 # 104,Suresh,Hyderabad,Developer,90000 # 101,Arman,Hyderabad,Developer,85000 # ... |
Multiple keys (sort by salary desc, then by name asc if salary same):
|
0 1 2 3 4 5 6 |
sort -t ',' -k 5,5nr -k 2,2 employees.txt |
5. Real-world examples you will use daily
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# Unique sorted usernames currently logged in who | cut -d ' ' -f 1 | sort -u # Top 10 biggest files in current folder du -sh * | sort -hr | head -10 # Sort access.log by IP (first field) awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -10 # Sort words in a file by frequency cat story.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr # Sort version numbers correctly (1.10 before 1.2) echo -e "1.2.3\n1.10.0\n1.9" | sort -V # Sort numerically but ignore first word (e.g. "Math 85") sort -k 2 -n scores.txt # English 78 Priya # Chemistry 65 Anjali # Math 85 Arman # ... |
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:
|
0 1 2 3 4 5 6 7 8 |
sort -f names.txt sort -t ',' -k 5 -nr employees.txt | head -3 cat scores.txt | sort -k 2 -n |
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! 😄
