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