1. Basic Alphabetical Sorting
$ sort fruits.txt
Input (fruits.txt):
apple
banana
cherry
date
elderberry
Output:
apple
banana
cherry
date
elderberry
2. Reverse Order Sorting
$ sort -r fruits.txt
Output:
elderberry
date
cherry
banana
apple
3. Numeric Sorting
$ sort -n numbers.txt
Input (numbers.txt):
10
2
25
100
5
Output:
2
5
10
25
100
4. Numeric Reverse Sorting
$ sort -nr numbers.txt
Output:
100
25
10
5
2
5. Case-Insensitive Sorting
$ sort -f mixed_case.txt
Input (mixed_case.txt):
Apple
banana
Cherry
date
Elderberry
Output:
Apple
banana
Cherry
date
Elderberry
6. Remove Duplicates While Sorting
$ sort -u duplicates.txt
Input (duplicates.txt):
apple
banana
apple
cherry
banana
date
Output:
apple
banana
cherry
date
7. Sort by Specific Column
$ sort -t, -k2 data.csv
Input (data.csv):
John,25,Engineer
Alice,30,Doctor
Bob,22,Student
Carol,28,Teacher
Output:
Bob,22,Student
John,25,Engineer
Carol,28,Teacher
Alice,30,Doctor
8. Sort by Multiple Columns
$ sort -k2 -k3n employees.txt
Input (employees.txt):
John Sales 50000
Alice IT 60000
Bob Sales 45000
Carol IT 55000
Output:
Carol IT 55000
Alice IT 60000
Bob Sales 45000
John Sales 50000
9. Human-Readable Number Sorting
$ sort -h sizes.txt
Input (sizes.txt):
1K
500
2M
100
5K
Output:
100
500
1K
5K
2M
10. Month Name Sorting
$ sort -M months.txt
Input (months.txt):
January
March
December
February
April
Output:
January
February
March
April
December
11. Check if File is Already Sorted
$ sort -c fruits.txt
Output (if sorted):
(No output)
Output (if not sorted):
sort: fruits.txt:3: disorder: cherry
12. Sort with Custom Field Separator
$ sort -t: -k2 colon_separated.txt
Input (colon_separated.txt):
apple:red:sweet
banana:yellow:sweet
cherry:red:sour
Output:
apple:red:sweet
cherry:red:sour
banana:yellow:sweet
13. Ignore Leading Blanks
$ sort -b with_spaces.txt
Input (with_spaces.txt):
apple
banana
cherry
date
Output:
apple
banana
cherry
date
14. Sort and Save to Output File
$ sort numbers.txt -o sorted_numbers.txt
Content of sorted_numbers.txt:
2
5
10
25
100
15. Version Number Sorting
$ sort -V versions.txt
Input (versions.txt):
1.10.2
1.2.1
1.1.0
2.0.0
1.9.5
Output:
1.1.0
1.2.1
1.9.5
1.10.2
2.0.0
16. Sort by String Length
$ cat fruits.txt | awk '{print length, $0}' | sort -n | cut -d" " -f2-
Output:
date
apple
banana
cherry
elderberry
17. Ignore Non-Printing Characters
$ printf "b\na\nc\n" | sort
Output:
a
b
c
18. Sort in Dictionary Order
$ sort -d with_punct.txt
Input (with_punct.txt):
apple!
banana?
cherry.
date,
Output:
apple!
banana?
cherry.
date,
19. Merge Already Sorted Files
$ sort -m file1.txt file2.txt
Input (file1.txt):
apple
cherry
Input (file2.txt):
banana
date
Output:
apple
banana
cherry
date
20. Sort with Random Order
$ sort -R fruits.txt
Output (varies each time):
banana
date
apple
elderberry
cherry
21. Sort by Numerical Value with Suffixes
$ sort -h with_suffix.txt
Input (with_suffix.txt):
10MB
1GB
100KB
2GB
500MB
Output:
100KB
10MB
500MB
1GB
2GB
22. Sort with Custom Locale
$ LC_ALL=C sort fruits.txt
Output:
apple
banana
cherry
date
elderberry
23. Sort and Show Line Numbers
$ cat -n fruits.txt | sort -k2
Output:
1 apple
2 banana
3 cherry
4 date
5 elderberry
24. Sort by Field with Different Data Types
$ sort -k1.2n mixed_data.txt
Input (mixed_data.txt):
A25
B100
C5
D1
E50
Output:
D1
C5
A25
E50
B100
25. Complex Multi-field Sorting
$ sort -t, -k3n -k4nr complex_data.txt
Input (complex_data.txt):
Smith,John,35,50000
Johnson,Alice,28,45000
Williams,Bob,35,55000
Brown,Carol,28,40000
Output:
Brown,Carol,28,40000
Johnson,Alice,28,45000
Smith,John,35,50000
Williams,Bob,35,55000