0
0
Linux CLIscripting~10 mins

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

Choose your learning style9 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.