sort and uniq in pipelines in Bash Scripting - Time & Space Complexity
When using sort and uniq together in a pipeline, it is important to understand how the time taken grows as the input size increases.
We want to know how the commands handle larger lists of data and how their work adds up.
Analyze the time complexity of the following code snippet.
sort input.txt | uniq > output.txt
This code sorts the lines of input.txt and then removes duplicate lines, saving the result to output.txt.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting all lines in the input file.
- How many times: The sorting process compares and rearranges lines multiple times, depending on input size.
- Secondary operation: The
uniqcommand scans the sorted output once to remove duplicates. - How many times: It reads through the sorted lines one by one.
As the number of lines grows, sorting takes more time because it must compare and reorder many lines.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 to 50 comparisons and rearrangements |
| 100 | About 700 to 1,000 comparisons and rearrangements |
| 1000 | About 10,000 to 15,000 comparisons and rearrangements |
Pattern observation: The sorting work grows faster than the input size, roughly multiplying by more than ten when input grows ten times. The uniq step grows linearly, reading each line once.
Time Complexity: O(n log n)
This means the time to sort and remove duplicates grows a bit faster than the number of lines, mainly because sorting takes more work as the list gets bigger.
[X] Wrong: "The uniq command alone removes duplicates from any list quickly without sorting first."
[OK] Correct: uniq only removes duplicates if they are next to each other. Without sorting first, duplicates scattered in the file won't be removed.
Understanding how sorting and filtering commands scale helps you reason about data processing in real scripts. This skill shows you can think about efficiency when working with pipelines.
What if we replaced sort with a command that only partially sorts the data? How would the time complexity change?
Practice
sort | uniq together in a bash pipeline?Solution
Step 1: Understand what
Thesortdoessortcommand arranges lines in alphabetical or numerical order.Step 2: Understand what
Theuniqdoes after sortinguniqcommand removes duplicate adjacent lines, so sorting first groups duplicates together for removal.Final Answer:
To get a sorted list with duplicates removed -> Option AQuick Check:
sort + uniq = sorted unique lines [OK]
- Using uniq before sort, which misses duplicates
- Thinking uniq counts lines instead of removing duplicates
- Assuming sort removes duplicates by itself
file.txt and sort the output?Solution
Step 1: Identify correct command order
The file must be sorted first, sosort file.txtcomes beforeuniq.Step 2: Check syntax correctness
sort file.txt | uniqcorrectly pipes sorted output to uniq for duplicate removal.Final Answer:
sort file.txt | uniq -> Option BQuick Check:
sort first, then uniq = correct syntax [OK]
- Placing uniq before sort, which fails to remove all duplicates
- Incorrect pipe placement causing syntax errors
- Trying to pass file as argument to uniq instead of sort
names.txt with contents:bob alice bob carol alice bob
What is the output of
sort names.txt | uniq?Solution
Step 1: Sort the file contents
Sorting lines alphabetically gives: alice, alice, bob, bob, bob, carol.Step 2: Apply uniq to remove duplicates
Uniq removes repeated adjacent lines, resulting in: alice, bob, carol.Final Answer:
alice\nbob\ncarol -> Option DQuick Check:
sort + uniq = unique sorted lines [OK]
- Not sorting first, so uniq misses duplicates
- Expecting original order instead of sorted
- Confusing uniq output with counting duplicates
uniq file.txt | sort to remove duplicates and sort lines, but duplicates remain. What is the likely problem?Solution
Step 1: Understand uniq behavior
Uniq removes only adjacent duplicate lines, so if duplicates are scattered, it won't remove them.Step 2: Analyze command order effect
Sorting after uniq means duplicates are not grouped before uniq runs, so duplicates remain.Final Answer:
uniq only removes adjacent duplicates, so sorting after uniq misses duplicates -> Option AQuick Check:
uniq needs sorted input to remove all duplicates [OK]
- Thinking uniq removes all duplicates regardless of order
- Believing sort alone removes duplicates
- Using uniq -c instead of uniq for removal
access.log with many repeated IP addresses scattered randomly. You want a sorted list of unique IPs. Which command pipeline correctly achieves this?Solution
Step 1: Understand
sort -uoptionsort -usorts and removes duplicates in one step efficiently.Step 2: Why others fail
Options A, B, and D all apply uniq before sorting, missing non-adjacent duplicates since IPs are scattered randomly.Final Answer:
sort -u access.log -> Option CQuick Check:
sort -u = sorted unique lines in one command [OK]
- Using uniq before sort, missing duplicates
- Not knowing sort -u combines sorting and uniqueness
- Using unnecessary cat command
Ready to prove it?
Pass the test and this pattern moves unknown
