sort and uniq in Linux CLI - Time & Space Complexity
When using sort and uniq commands together, it is important to understand how the time taken grows as the input size increases.
We want to know how the execution time changes when the input file gets bigger.
Analyze the time complexity of the following code snippet.
sort input.txt | uniq
This code sorts the lines of a file and then removes duplicate lines that are next to each other.
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:
uniqscans the sorted lines once to remove duplicates. - How many times: One pass through all lines.
As the number of lines grows, sorting takes more time because it compares many pairs of lines. Removing duplicates is faster since it just checks neighbors once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 to 50 comparisons for sorting, plus 10 checks for duplicates |
| 100 | About 700 to 1000 comparisons for sorting, plus 100 checks for duplicates |
| 1000 | About 10,000 to 15,000 comparisons for sorting, plus 1000 checks for duplicates |
Pattern observation: Sorting grows faster than linearly, while duplicate removal grows linearly.
Time Complexity: O(n log n)
This means the time to sort and remove duplicates grows a bit faster than just the number of lines, mainly because sorting takes more work as the file gets bigger.
[X] Wrong: "The whole command runs in linear time because uniq just scans once."
[OK] Correct: Sorting is the main cost and it takes more than linear time, so the total time is dominated by sorting, not just scanning.
Understanding how sorting affects performance helps you explain real-world scripts and commands clearly. This skill shows you can think about how tools behave as data grows.
"What if we used sort -u instead of sort | uniq? How would the time complexity change?"
Practice
uniq command do when used alone on a text file?Solution
Step 1: Understand the uniq command behavior
Theuniqcommand filters out repeated lines only if they are next to each other.Step 2: Compare with other options
It does not sort lines or count all lines; it only removes adjacent duplicates.Final Answer:
Removes repeated adjacent lines -> Option BQuick Check:
uniq removes adjacent duplicates = D [OK]
- Thinking uniq sorts lines
- Assuming uniq removes all duplicates anywhere
- Confusing uniq with wc (word count)
data.txt and remove duplicate lines?Solution
Step 1: Understand the order of commands
To remove duplicates correctly, lines must be sorted first so duplicates become adjacent.Step 2: Check each option
sort data.txt | uniq sorts then pipes to uniq, which removes adjacent duplicates. uniq data.txt | sort runs uniq first, which won't remove all duplicates if unsorted. sort -u data.txt uses sort -u which is valid but not the pipe syntax asked. uniq -s data.txt is invalid syntax.Final Answer:
sort data.txt | uniq -> Option AQuick Check:
Sort before uniq to remove duplicates = A [OK]
- Running uniq before sort
- Using invalid uniq options
- Confusing sort -u with uniq
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 lines
Sorting the lines alphabetically gives: alice, alice, bob, bob, bob, carol.Step 2: Apply uniq to remove adjacent duplicates
uniq removes repeated adjacent lines, so only one of each remains: alice, bob, carol.Final Answer:
alice bob carol -> Option AQuick Check:
Sort then uniq removes duplicates = alice, bob, carol [OK]
- Not sorting before uniq
- Expecting uniq to remove all duplicates anywhere
- Confusing output order
sort data.txt | uniq but notice duplicates remain. What is the most likely cause?Solution
Step 1: Understand how sort and uniq treat whitespace
sort and uniq treat lines with trailing spaces or tabs as different lines, so duplicates with extra spaces won't be adjacent identical.Step 2: Check other options
uniq misspelling would cause error, not duplicates remaining. sort works on text files. Empty file means no duplicates.Final Answer:
The file contains lines with trailing spaces or tabs -> Option CQuick Check:
Trailing spaces cause uniq to miss duplicates = C [OK]
- Assuming uniq always removes duplicates regardless of spaces
- Ignoring trailing whitespace differences
- Thinking sort fails on text files
access.log. Which command correctly sorts, removes duplicates, and counts unique lines?Solution
Step 1: Understand the goal
You want the count of unique IP addresses, so you need to remove duplicates and count the unique lines.Step 2: Analyze each option
sort access.log | uniq -c counts occurrences but does not sum total unique lines. uniq -c access.log | sort counts duplicates before sorting, so misses some. sort -u access.log | wc -l usessort -uto get unique lines, then counts lines withwc -l, which gives the unique count. uniq access.log | wc -l counts lines after uniq without sorting, so duplicates may remain.Final Answer:
sort -u access.log | wc -l -> Option DQuick Check:
sort -u then wc -l counts unique lines = B [OK]
- Using uniq -c without sorting first
- Counting lines before removing duplicates
- Confusing uniq -c output with total unique count
Ready to prove it?
Pass the test and this pattern moves unknown
