fruits.txt containing:apple banana apple orange banana apple?
sort fruits.txt | uniq
sort orders lines alphabetically and uniq removes adjacent duplicates.The sort command arranges the lines alphabetically, so the file becomes:apple. Then
apple
apple
banana
banana
orangeuniq removes repeated adjacent lines, leaving one of each unique line in order.
colors.txt with content:red blue red green blue red
What is the output of the command
uniq colors.txt?uniq colors.txt
uniq only removes adjacent duplicate lines.Since the file is not sorted, duplicate lines are not adjacent. uniq does not remove non-adjacent duplicates, so the output is the same as the input.
data.txt?uniq -c counts adjacent duplicates only. Sorting first groups duplicates together, so sort data.txt | uniq -c correctly counts all duplicates.
uniq -u data.txt to get unique lines from data.txt but sees duplicates still present. Why?uniq works with adjacent lines.uniq -u prints only lines that are unique and not repeated adjacent lines. If the file is not sorted, duplicates may not be adjacent, so they remain.
access.log with many lines, each starting with an IP address. Which command extracts all unique IP addresses sorted alphabetically?First, cut -d' ' -f1 extracts the IP addresses. Then sort arranges them alphabetically, grouping duplicates. Finally, uniq removes duplicates.