Linux command
uniq 命令
文件
涉及管道、覆盖或删除,执行前请先确认路径和参数。
常用示例
Remove adjacent duplicate lines
sort [file] | uniq
Show only duplicate lines
sort [file] | uniq -d
Show only unique lines
sort [file] | uniq -u
Count occurrences
sort [file] | uniq -c
Ignore case when comparing
sort [file] | uniq -i
Skip first N fields
sort [file] | uniq -f [N]
Skip first N characters
sort [file] | uniq -s [N]
说明
uniq filters adjacent matching lines from input, writing unique lines to output. It only compares consecutive lines, so input typically needs to be sorted first. Commonly used with sort in a pipeline: sort file | uniq removes all duplicates. Adding -c shows frequency counts, useful for analyzing log files or finding common patterns. The -d option shows only lines that appear more than once, while -u shows only lines appearing exactly once. These are mutually exclusive perspectives on the data. Field and character skipping options allow ignoring prefixes (like timestamps or line numbers) when comparing for uniqueness.
参数
- -c, --count
- Prefix lines with occurrence count
- -d, --repeated
- Only print duplicate lines, one per group
- -D
- Print all duplicate lines
- -u, --unique
- Only print unique lines (occurring once)
- -i, --ignore-case
- Ignore case when comparing
- -f _N_, --skip-fields=_N_
- Skip first N fields when comparing
- -s _N_, --skip-chars=_N_
- Skip first N characters when comparing
- -w _N_, --check-chars=_N_
- Compare only first N characters
- -z, --zero-terminated
- Line delimiter is NUL, not newline
FAQ
What is the uniq command used for?
uniq filters adjacent matching lines from input, writing unique lines to output. It only compares consecutive lines, so input typically needs to be sorted first. Commonly used with sort in a pipeline: sort file | uniq removes all duplicates. Adding -c shows frequency counts, useful for analyzing log files or finding common patterns. The -d option shows only lines that appear more than once, while -u shows only lines appearing exactly once. These are mutually exclusive perspectives on the data. Field and character skipping options allow ignoring prefixes (like timestamps or line numbers) when comparing for uniqueness.
How do I run a basic uniq example?
Run `sort [file] | uniq` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -c, --count do in uniq?
Prefix lines with occurrence count