Text processing

Linux uniq command

Report or remove adjacent repeated lines; sort first to group matches.

Availability: Common Linux commands; shell built-ins and external implementations may differ by distribution.中文

Syntax

uniq [OPTION]... [INPUT [OUTPUT]]

Examples

Count each distinct line; uniq compares adjacent lines.

sort names.txt | uniq -c

Print values that occur more than once.

sort names.txt | uniq -d

Parameters, options and clauses

Option / clauseDescription
-c, --countPrefix each output line with its number of occurrences.
-d, --repeatedPrint only repeated lines.
-u, --uniquePrint only lines that occur once.
-i, --ignore-caseIgnore case when comparing lines.
-f, --skip-fields=NSkip N fields before comparing.
-s, --skip-chars=NSkip N characters before comparing.
Safety note

Review a command and its target before running it. Commands that alter files, services, accounts, disks, permissions or databases may be difficult to reverse. Test in a safe environment and keep backups.

Official reference ↗Back to directory