0
0
Bash Scriptingscripting~20 mins

sort and uniq in pipelines in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pipeline Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this pipeline?
Consider a file fruits.txt with the following lines:
apple
banana
apple
orange
banana
banana

What is the output of this command?
cat fruits.txt | sort | uniq
Bash Scripting
cat fruits.txt | sort | uniq
A
apple
banana
banana
orange
B
apple
apple
banana
banana
banana
orange
C
apple
banana
orange
D
banana
apple
orange
Attempts:
2 left
💡 Hint
Think about what sort and uniq do when combined.
💻 Command Output
intermediate
2:00remaining
What does this pipeline output?
Given a file colors.txt with:
red
blue
red
green
blue
blue

What is the output of:
cat colors.txt | uniq | sort
Bash Scripting
cat colors.txt | uniq | sort
A
blue
blue
green
red
red
B
red
blue
green
C
red
blue
red
green
blue
blue
D
red
green
blue
Attempts:
2 left
💡 Hint
Remember that uniq only removes adjacent duplicates.
📝 Syntax
advanced
2:00remaining
Which pipeline correctly counts unique lines ignoring case?
You want to count how many unique lines are in data.txt, ignoring case differences. Which command pipeline does this correctly?
Acat data.txt | sort -f | uniq -i | wc -l
Bcat data.txt | sort | uniq -i | wc -l
Ccat data.txt | uniq -i | sort | wc -l
Dcat data.txt | sort -f | uniq | wc -l
Attempts:
2 left
💡 Hint
Check which commands support case-insensitive options and how they affect sorting and uniqueness.
🔧 Debug
advanced
2:00remaining
Why does this pipeline not remove all duplicates?
You run this command:
cat list.txt | uniq | sort

But you notice duplicates remain in the output. Why?
ABecause <code>cat</code> removes duplicates before piping.
BBecause <code>uniq</code> only removes adjacent duplicates, and sorting after <code>uniq</code> does not group duplicates together.
CBecause <code>uniq</code> requires the <code>-d</code> option to remove duplicates.
DBecause <code>sort</code> does not work on piped input.
Attempts:
2 left
💡 Hint
Think about the order of commands and how uniq works.
🚀 Application
expert
3:00remaining
How to find the top 3 most frequent words in a file?
You have a file words.txt with many words, one per line. You want to find the top 3 most frequent words along with their counts. Which pipeline achieves this?
Acat words.txt | sort | uniq | sort -nr | head -3
Bcat words.txt | uniq -c | sort -nr | head -3
Ccat words.txt | sort -nr | uniq -c | head -3
Dcat words.txt | sort | uniq -c | sort -nr | head -3
Attempts:
2 left
💡 Hint
Count duplicates after sorting, then sort counts numerically in reverse order.