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.
echo -e "apple\nbanana\napple\ncarrot" | sort | uniq| Step | Input | Command | Output | Notes |
|---|---|---|---|---|
| 1 | apple\nbanana\napple\ncarrot | echo input | apple\nbanana\napple\ncarrot | Original unsorted input with duplicates |
| 2 | apple\nbanana\napple\ncarrot | sort | apple\napple\nbanana\ncarrot | Lines sorted alphabetically |
| 3 | apple\napple\nbanana\ncarrot | uniq | apple\nbanana\ncarrot | Consecutive duplicates removed |
| 4 | apple\nbanana\ncarrot | end | apple\nbanana\ncarrot | Final unique sorted output |
| Variable | Start | After sort | After uniq | Final |
|---|---|---|---|---|
| lines | apple\nbanana\napple\ncarrot | apple\napple\nbanana\ncarrot | apple\nbanana\ncarrot | apple\nbanana\ncarrot |
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
uniq command do when used alone on a text file?uniq command filters out repeated lines only if they are next to each other.data.txt and remove duplicate lines?names.txt with contents:bob alice bob carol alice bob
sort names.txt | uniq?sort data.txt | uniq but notice duplicates remain. What is the most likely cause?access.log. Which command correctly sorts, removes duplicates, and counts unique lines?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.Pass the test and this pattern moves unknown