Bird
Raised Fist0
Linux CLIscripting~10 mins

sort and uniq in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - sort and uniq
Input lines
↓
sort command
↓
sorted lines
↓
uniq command
↓
unique lines output
The input lines are first sorted alphabetically, then passed to uniq which removes consecutive duplicate lines, producing unique sorted output.
Execution Sample
Linux CLI
echo -e "apple\nbanana\napple\ncarrot" | sort | uniq
Sorts the list of words and removes duplicates, showing unique sorted words.
Execution Table
StepInputCommandOutputNotes
1apple\nbanana\napple\ncarrotecho inputapple\nbanana\napple\ncarrotOriginal unsorted input with duplicates
2apple\nbanana\napple\ncarrotsortapple\napple\nbanana\ncarrotLines sorted alphabetically
3apple\napple\nbanana\ncarrotuniqapple\nbanana\ncarrotConsecutive duplicates removed
4apple\nbanana\ncarrotendapple\nbanana\ncarrotFinal unique sorted output
💡 uniq stops after processing all sorted lines, outputting unique lines
Variable Tracker
VariableStartAfter sortAfter uniqFinal
linesapple\nbanana\napple\ncarrotapple\napple\nbanana\ncarrotapple\nbanana\ncarrotapple\nbanana\ncarrot
Key Moments - 2 Insights
Why does uniq only remove duplicates after sorting?
uniq only removes duplicates that are next to each other. Sorting puts duplicates next to each other, so uniq can remove them. See execution_table rows 2 and 3.
What happens if you run uniq without sort?
uniq will only remove duplicates that are consecutive in the original input. Non-adjacent duplicates stay. This is why sorting first is important. See execution_table row 1 vs 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after the sort command?
Aapple\nbanana\ncarrot
Bapple\nbanana\napple\ncarrot
Capple\napple\nbanana\ncarrot
Dbanana\napple\ncarrot
💡 Hint
Check the 'Output' column in step 2 of the execution_table.
At which step does uniq remove duplicates?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Command' and 'Output' columns in execution_table rows 2 and 3.
If the input was already sorted with duplicates together, what would change in the execution table?
Auniq would not remove any lines
BThe sort step would output the same as input
CThe final output would have duplicates
DThe input would be empty
💡 Hint
Consider what sorting does if input is already sorted (see variable_tracker).
Concept Snapshot
sort and uniq commands:
- sort: arranges lines alphabetically
- uniq: removes only adjacent duplicate lines
- Use together: sort | uniq
- Ensures unique sorted output
- uniq alone won't remove non-adjacent duplicates
Full Transcript
This visual trace shows how the Linux commands sort and uniq work together. First, the input lines are printed as is. Then sort arranges them alphabetically, putting duplicates next to each other. Next, uniq removes these consecutive duplicates, leaving only unique lines. The final output is unique sorted lines. This is important because uniq only removes duplicates that are next to each other, so sorting first is necessary to group duplicates. Running uniq without sorting only removes duplicates that are already adjacent. The execution table and variable tracker clearly show each step's input and output, helping beginners see how the commands transform the data step-by-step.

Practice

(1/5)
1. What does the uniq command do when used alone on a text file?
easy
A. Deletes all empty lines
B. Removes repeated adjacent lines
C. Counts all lines in the file
D. Sorts all lines alphabetically

Solution

  1. Step 1: Understand the uniq command behavior

    The uniq command filters out repeated lines only if they are next to each other.
  2. Step 2: Compare with other options

    It does not sort lines or count all lines; it only removes adjacent duplicates.
  3. Final Answer:

    Removes repeated adjacent lines -> Option B
  4. Quick Check:

    uniq removes adjacent duplicates = D [OK]
Hint: uniq only removes duplicates if they are next to each other [OK]
Common Mistakes:
  • Thinking uniq sorts lines
  • Assuming uniq removes all duplicates anywhere
  • Confusing uniq with wc (word count)
2. Which of the following is the correct syntax using a pipe to sort a file named data.txt and remove duplicate lines?
easy
A. sort data.txt | uniq
B. uniq data.txt | sort
C. sort -u data.txt
D. uniq -s data.txt

Solution

  1. Step 1: Understand the order of commands

    To remove duplicates correctly, lines must be sorted first so duplicates become adjacent.
  2. 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.
  3. Final Answer:

    sort data.txt | uniq -> Option A
  4. Quick Check:

    Sort before uniq to remove duplicates = A [OK]
Hint: Always sort before uniq to remove duplicates correctly [OK]
Common Mistakes:
  • Running uniq before sort
  • Using invalid uniq options
  • Confusing sort -u with uniq
3. Given a file names.txt with contents:
bob
alice
bob
carol
alice
bob

What is the output of sort names.txt | uniq?
medium
A. alice bob carol
B. bob alice carol
C. alice bob bob carol alice bob
D. bob carol alice

Solution

  1. Step 1: Sort the file lines

    Sorting the lines alphabetically gives: alice, alice, bob, bob, bob, carol.
  2. Step 2: Apply uniq to remove adjacent duplicates

    uniq removes repeated adjacent lines, so only one of each remains: alice, bob, carol.
  3. Final Answer:

    alice bob carol -> Option A
  4. Quick Check:

    Sort then uniq removes duplicates = alice, bob, carol [OK]
Hint: Sort first, then uniq removes duplicates in order [OK]
Common Mistakes:
  • Not sorting before uniq
  • Expecting uniq to remove all duplicates anywhere
  • Confusing output order
4. You run sort data.txt | uniq but notice duplicates remain. What is the most likely cause?
medium
A. sort command does not work on text files
B. uniq command is misspelled
C. The file contains lines with trailing spaces or tabs
D. The file is empty

Solution

  1. 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.
  2. Step 2: Check other options

    uniq misspelling would cause error, not duplicates remaining. sort works on text files. Empty file means no duplicates.
  3. Final Answer:

    The file contains lines with trailing spaces or tabs -> Option C
  4. Quick Check:

    Trailing spaces cause uniq to miss duplicates = C [OK]
Hint: Check for trailing spaces if duplicates remain after sort|uniq [OK]
Common Mistakes:
  • Assuming uniq always removes duplicates regardless of spaces
  • Ignoring trailing whitespace differences
  • Thinking sort fails on text files
5. You want to count how many unique IP addresses appear in access.log. Which command correctly sorts, removes duplicates, and counts unique lines?
hard
A. sort access.log | uniq -c
B. uniq access.log | wc -l
C. uniq -c access.log | sort
D. sort -u access.log | wc -l

Solution

  1. Step 1: Understand the goal

    You want the count of unique IP addresses, so you need to remove duplicates and count the unique lines.
  2. 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 uses sort -u to get unique lines, then counts lines with wc -l, which gives the unique count. uniq access.log | wc -l counts lines after uniq without sorting, so duplicates may remain.
  3. Final Answer:

    sort -u access.log | wc -l -> Option D
  4. Quick Check:

    sort -u then wc -l counts unique lines = B [OK]
Hint: Use sort -u then wc -l to count unique lines fast [OK]
Common Mistakes:
  • Using uniq -c without sorting first
  • Counting lines before removing duplicates
  • Confusing uniq -c output with total unique count
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard