Bird
0
0

Why does the command sort file.txt | uniq remove duplicates correctly, but uniq file.txt alone may not?

hard🧠 Conceptual Q10 of 15
Bash Scripting - Text Processing in Scripts
Why does the command sort file.txt | uniq remove duplicates correctly, but uniq file.txt alone may not?
Asort removes duplicates before uniq runs
Buniq requires sorted input to run without errors
Cuniq cannot read files directly, only from pipes
Duniq only removes duplicates if they are adjacent, sorting ensures adjacency
Step-by-Step Solution
Solution:
  1. Step 1: Understand uniq's behavior

    uniq removes only adjacent duplicate lines.
  2. Step 2: Role of sort

    sort arranges duplicates next to each other, enabling uniq to remove them.
  3. Final Answer:

    uniq only removes duplicates if they are adjacent, sorting ensures adjacency -> Option D
  4. Quick Check:

    uniq needs sorted input to remove all duplicates [OK]
Quick Trick: uniq removes only adjacent duplicates; sort first [OK]
Common Mistakes:
MISTAKES
  • Thinking uniq removes all duplicates regardless of order
  • Believing uniq requires sorted input to avoid errors
  • Assuming sort removes duplicates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes