We can sort data in a file using the sort command:
sort filenameThis sorts the content based on alphabetical order.
akshay@Ubuntu:~/Desktopcat a.txt
Sunny
Bunny
Chinny
Pinny
Vinny
akshay@Ubuntu:~/Desktopsort a.txt
Bunny
Chinny
Pinny
Sunny
Vinny- To save the sorted content into another file (
sorted.txt), use:
sort a.txt > sorted.txtTo sort the content in reverse alphabetical order, use the -r option:
sort -r a.txt > sorted.txtTo remove duplicate lines from a file, use:
sort -u a.txt | tac > sorted.txt
sort -ru a.txt > sorted.txtsort a.txt
Bunny
Chinny
Pinny
Sunny
Vinnysort -r a.txt
Vinny
Sunny
Pinny
Chinny
Bunnycat a.txt
7
Sunny
8
Bunny
1
Chinny
6
Vinny
5
Pinny
sort a.txt
1
5
6
7
8
Bunny
Chinny
Pinny
Sunny
Vinnysort -r a.txt
Vinny
Sunny
Pinny
Chinny
Bunny
8
7
6
5
1By default, the sort command does not sort numbers numerically but based on their string (digit-wise) representation.
cat a.txt
11
2
7
2222222
9
sort a.txt
11
2
2222222
7
9sort -n a.txt
2
7
9
11
2222222By default, sort includes all lines, including duplicates. Use the -u option to display only unique lines:
cat a.txt
1
1
2
2
Sunny
Sunny
Bunny
sort -u a.txt
1
2
Bunny
Sunny-
Sort Based on File Size (Ascending Order):
ls -l /etc | head -10 | sort -k 5n
This command sorts based on the 5th column (file size) in ascending order.
-
Sort Based on Month:
ls -l /etc | head -10 | sort -k 6M
This sorts by the 6th column (month) in ascending order.
-
Sort Based on Number of Links (Descending Order):
ls -l /etc | head -10 | sort -k 2nr
Sorts based on the 2nd column (number of links) in descending order.
The -k option is used to sort based on a specific column.
Example:
ls -l /etc | head -10 | sort -k 5nThis sorts based on file size (5th column) in ascending order.
ls -l /etc | head -10 | sort -k 2nremp.txt file content:
800: Avinash:500:Warangal
100: Yashwant:1000:Mumbai
200: Omkar:2000:Chennai
300: Chinmay:3000:Hyderabad
600: Shreya:6000:Hyderabad
400: Vaibhav:4000:Delhi
500: Pragati:5000:Hyderabad
700: Shailesh:7000:Ranchi
-
Find Lowest Salary Record:
sort -k 3n -t ":" emp.txt | head -1
This finds the employee with the lowest salary.
-
Find Highest Salary Record:
sort -k 3nr -t ":" emp.txt | head -1
This finds the employee with the highest salary.
The uniq command is used to display unique lines. However, the file needs to be sorted for uniq to work properly.
cat a.txt
Sunny
sunny
Bunny
Chinny
Sunny
Bunny
Chinny
uniq a.txt
Sunny
sunny
Bunny
Chinny
Sunny
Bunny
ChinnyWithout sorting, uniq doesn't remove duplicates correctly.
sort a.txt | uniq
Bunny
Chinny
sunny
Sunny-
To Display Only Duplicate Lines:
sort a.txt | uniq -d Bunny Chinny Sunny -
To Display the Number of Occurrences of Each Line:
sort a.txt | uniq -c 2 Bunny 2 Chinny 1 sunny 2 Sunny -
To Ignore Case While Comparing:
sort a.txt | uniq -i Bunny Chinny sunny Sunny -
To Display Only Unique Lines (Lines Not Duplicated):
sort a.txt | uniq -u sunny