0
0
Linux CLIscripting~20 mins

sort and uniq in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sort & Uniq Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of combined sort and uniq commands
What is the output of the following command sequence on the file fruits.txt containing:
apple
banana
apple
orange
banana
apple
?
Linux CLI
sort fruits.txt | uniq
A
apple
apple
banana
banana
orange
B
apple
banana
orange
C
apple
banana
apple
orange
banana
apple
D
banana
apple
orange
Attempts:
2 left
💡 Hint
Remember that sort orders lines alphabetically and uniq removes adjacent duplicates.
💻 Command Output
intermediate
2:00remaining
Effect of uniq without sorting
Given a file colors.txt with content:
red
blue
red
green
blue
red

What is the output of the command uniq colors.txt?
Linux CLI
uniq colors.txt
A
red
blue
red
green
blue
red
red
B
red
blue
green
C
red
blue
red
green
blue
D
red
blue
red
green
blue
red
Attempts:
2 left
💡 Hint
uniq only removes adjacent duplicate lines.
📝 Syntax
advanced
2:00remaining
Correct usage of uniq to count duplicates
Which command correctly counts how many times each unique line appears in data.txt?
Asort data.txt | uniq -c
Buniq -c data.txt
Csort -c data.txt | uniq -c
Duniq data.txt | sort -c
Attempts:
2 left
💡 Hint
Counting duplicates requires sorting first so duplicates are adjacent.
🔧 Debug
advanced
2:00remaining
Why does this command not remove all duplicates?
A user runs uniq -u data.txt to get unique lines from data.txt but sees duplicates still present. Why?
ABecause <code>uniq</code> only removes adjacent duplicates; the file is not sorted.
BBecause <code>-u</code> option is invalid and ignored.
CBecause <code>uniq</code> removes all duplicates regardless of order.
DBecause the file contains hidden characters causing errors.
Attempts:
2 left
💡 Hint
Think about how uniq works with adjacent lines.
🚀 Application
expert
3:00remaining
Extract unique sorted IP addresses from a log
You have a log file access.log with many lines, each starting with an IP address. Which command extracts all unique IP addresses sorted alphabetically?
Asort access.log | cut -d' ' -f1 | uniq
Buniq access.log | cut -d' ' -f1 | sort
Ccut -d' ' -f1 access.log | sort | uniq
Dcut -d' ' -f1 access.log | uniq | sort
Attempts:
2 left
💡 Hint
Extract the first field, then sort, then remove duplicates.