Go to main content

手册页部分 1:用户命令

退出打印视图

更新时间: 2022年7月27日 星期三
 
 

uniq(1)

名称

uniq - 报告或过滤出文件中重复的行

用法概要

/usr/bin/uniq [ [-c | --count] | [-d | --repeated] | [-u | --unique]
            | [-D[delimit-method] | --all-repeated[=delimit-method]] ]
     [-f  fields | --skip-fields=fields] [-s chars |--skip-chars=chars]
     [-w | --check-chars=chars] [-i | --ignore-case]
     [-z | --zero-terminated] [input_file [output_file]]
/usr/bin/uniq [ [-c | --count] | [-d | --repeated] | [-u | --unique]
            | [-D[delimit-method] | --all-repeated[=delimit-method]] ]
     [-f  fields | --skip-fields=fields] [-s chars |--skip-chars=chars]
     [-w | --check-chars=chars] [-i | --ignore-case]
     [-z | --zero-terminated] [-n] [+m][input_file [output_file]]
/usr/bin/uniq --help

描述

uniq 可以读取输入、比较相邻的行并将每个输入行的一个副本写入输出。不会写入重复相邻行的第二个及以后的副本。

如果未指定输出文件 output_fileuniq 将写入标准输出。如果未指定 input_file 或者 input_file-uniq 将从标准输入读取,并将文件开头定义为当前偏移。

选项

支持以下选项:

–c, –-count

在每个输出行前面添加该行在输入中出现次数的计数。

–d, –-repeated

不写入输入中不重复的行。

–D, –-all-repeated[=delimit-method]

使用以下分隔方法之一分隔重复行组:

none (default)

不进行分隔。

prepend

在每个组前面附加一个分隔符。

separate

使用分隔符分隔组。

使用空白行进行分隔。

–f, –skip-fields=fields

Ignores the first fields fields on each input line when doing comparisons, where fields is a positive decimal integer.field 是符合以下基本正则表达式的最大字符串:

[[:blank:]]*[^[:blank:]]*

If fields specifies more fields than appear on an input line, a null string are used for comparison.

–i, –-ignore-case

进行比较时忽略大小写差异。

–s, –-skip-chars=chars

进行比较时,忽略第一个 chars 字符(chars 是一个十进制正整数)。If specified in conjunction with the –f option, the first chars characters after the first fields fields are ignored.If chars specifies more characters than remain on an input line, a null string are used for comparison.

–u, –-unique

不写入输入中重复的行。

–w, –-check-chars=chars

Compare no more than char characters in a line.

–z, –-zero-terminated

使用 0 字节(而非换行符)结束行。

-n

等效于将 fields 设置为 n 时的 –f fields

+m

等效于将 chars 设置为 m 时的 –s chars

–-help

显示命令用法并退出。

操作数

支持下列操作数:

input_file

输入文件的路径名。如果未指定 input_file 或者 input_file- ,则使用标准输入。

output_file

输出文件的路径名。如果未指定 output_file,则使用标准输出。如果 output_file 指定的文件是 input_file 指定的文件,则产生的结果不确定。

示例

示例 1 使用 uniq 命令

以下示例将列出 uniq.test 文件的内容,并输出重复行的一个副本。

example% cat uniq.test
This is a test.
This is a test.
TEST.
Computer.
TEST.
TEST.
Software.

example% uniq -d uniq.test
This is a test.
TEST.
example%

接下来这个示例仅输出 uniq.test 文件中不重复的行。

example% uniq -u uniq.test
TEST.
Computer.
Software.
example%

最后一个示例输出一份报告,每一行前面有该行在文件中出现次数的计数:

example% uniq -c uniq.test
   2 This is a test.
   1 TEST.
   1 Computer.
   2 TEST.
   1 Software.
example%

环境变量

See environ(7) for descriptions of the following environment variables that affect the execution of uniq: LANG, LC_ALL, LC_CTYPE, LC_MESSAGES, and NLSPATH.

退出状态

将返回以下退出值:

0

成功完成。

> 0

出现错误。

属性

有关下列属性的说明,请参见 attributes(7)

属性类型
属性值
可用性
system/core-os
CSI
Enabled(已启用)
接口稳定性
Committed(已确定)
标准
请参见 standards(7)

另请参见

comm(1), sort(1), attributes(7), environ(7), standards(7)