Using uniq and sort in Linux
The uniq and sort commands are powerful tools in Linux for processing and organizing text data. They are often used together to remove duplicates and sort data efficiently.sort Command
The sort command sorts lines of text files. By default, it sorts in ascending order.Basic Usage:
Code:
sort filename.txt
Sort in Descending Order:
Code:
sort -r filename.txt
uniq Command
The uniq command filters out repeated lines in a file. It only removes adjacent duplicate lines, so it is often used with sort.Basic Usage:
Code:
uniq filename.txt
Count Occurrences:
Code:
uniq -c filename.txt
Combining sort and uniq
To remove all duplicate lines from a file, you can combine sort and uniq.Remove Duplicates:
Code:
sort filename.txt | uniq
Count Unique Lines:
Code:
sort filename.txt | uniq -c
Sorting Text with cut or awk
You can use cut or awk to extract specific columns from a file and then sort them.Using cut:
Code:
cut -d',' -f2 filename.csv | sort
Using awk:
Code:
awk -F',' '{print $2}' filename.csv | sort
Additional Suggestions
- Sort by Numeric Values:
Code:
sort -n filename.txt
- Sort by Month:
Code:
sort -M filename.txt
- Sort by a Specific Column:
Code:
sort -k 2 filename.txt
- Remove Duplicate Lines and Save to a New File:
Code:
sort filename.txt | uniq > newfile.txt