The uniq command in Linux is used to filter out duplicate lines in text files. Here's how it works:
-
Basic Functionality: By default,
uniqremoves adjacent duplicate lines from a file. This means that only consecutive duplicates are filtered out. -
Sorting Requirement: To remove all duplicates regardless of their position in the file, you typically need to sort the file first using the
sortcommand. For example:sort input.txt | uniq -
Options:
-c: Counts occurrences of each unique line and displays the count.-d: Shows only the duplicate lines.-u: Displays only unique lines (lines that appear exactly once).-i: Ignores case when comparing lines.-f N: Skips the first N fields when comparing lines.-s N: Skips the first N characters when comparing lines.
These features make uniq a powerful tool for data processing and analysis in text files.
