Linux / Bash
Beginner
1 min read
Cutting, Sorting, and Deduplication with cut, sort, and uniq
Example
# ---- cut examples ----
# Extract field 1 (username) from /etc/passwd (: delimiter)
cut -d: -f1 /etc/passwd
# Extract fields 1 and 7 (username and shell)
cut -d: -f1,7 /etc/passwd
# Extract characters 1-10 from each line
cut -c1-10 /var/log/syslog
# Extract from character 15 to end of line
cut -c15- /var/log/syslog
# ---- sort examples ----
# Alphabetical sort
sort /etc/passwd
# Numeric sort (field 3 = UID), colon delimiter
sort -t: -k3 -n /etc/passwd
# Reverse sort (largest UID first)
sort -t: -k3 -rn /etc/passwd
# Sort by file size (second column of du output)
du -sh /var/log/* | sort -h
# Remove duplicate lines during sort
sort -u words.txt
# Human-readable sort (1K, 10M, 2G)
df -h | sort -k5 -h
# ---- uniq examples ----
# Count occurrences of each line
sort access.log | uniq -c
# Sort by frequency (most common first)
sort access.log | uniq -c | sort -rn | head -20
# Show only lines that appear more than once
sort file.txt | uniq -d
# Show only lines that appear exactly once
sort file.txt | uniq -u
# Full pipeline: top 10 IPs hitting your web server
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10
Related Resources
Linux / Bash Reference
Complete tag & property list
Linux / Bash How-To Guides
Step-by-step practical guides
Linux / Bash Exercises
Practice what you've learned
More in Linux / Bash