Concept Flow - sort and uniq in pipelines
Input lines
sort command
uniq command
Output unique sorted lines
The pipeline takes input lines, sorts them alphabetically, then filters out duplicates to output unique sorted lines.
printf "apple\nbanana\napple\ncherry\nbanana\n" | sort | uniq| Step | Command | Input | Output | Explanation |
|---|---|---|---|---|
| 1 | printf | none | apple banana apple cherry banana | Prints the list of fruits with duplicates |
| 2 | sort | apple banana apple cherry banana | apple apple banana banana cherry | Sorts lines alphabetically, duplicates stay |
| 3 | uniq | apple apple banana banana cherry | apple banana cherry | Removes adjacent duplicate lines |
| 4 | end | N/A | apple banana cherry | Pipeline ends with unique sorted output |
| Variable | Start | After printf | After sort | After uniq | Final |
|---|---|---|---|---|---|
| lines | empty | apple banana apple cherry banana | apple apple banana banana cherry | apple banana cherry | apple banana cherry |
Use 'sort | uniq' in a pipeline to get unique sorted lines. 'sort' arranges lines alphabetically. 'uniq' removes only adjacent duplicates. Pipe output of sort into uniq for correct unique filtering. Without sort, uniq misses non-adjacent duplicates.