Go to main content

手册页部分 1:用户命令

退出打印视图

更新时间: 2018年8月8日 星期三
 
 

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][--help] [
input_file [output_file]]
/usr/bin/uniq [ [-c | --count
] |[-d | --repeated
] |[-u | --unique] |[
-D[delimit-method] | --all-repeated[=
delimit-method]] ][-w, 
--check-chars=chars][
-i | --ignore-case] [-z | 
--zero-terminated][--help][
-n ] [+m ][input_file
 [output_file]]

描述

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

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

选项

支持以下选项:

–c, –-count

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

–d, –-repeated

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

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

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

none(缺省值)

不进行分隔。

prepend

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

separate

使用分隔符分隔组。

使用空白行进行分隔。

–f, –skip-fields=fields

进行比较时,忽略每个输入行中的第一个 fields fieldsfields 是一个十进制正整数)。field 是符合以下基本正则表达式的最大字符串:

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

如果 fields 指定的 fields 多于输入行中出现的数目,则使用空字符串进行比较。

–i–-ignore-case

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

–s–-skip-chars=chars

进行比较时,忽略第一个 chars 字符(chars 是一个十进制正整数)。如果与 –f 选项一起指定,将忽略第一个 fields fields 后面的第一个 chars 字符。如果 chars 指定的字符多于输入行中存在的数目,则使用空字符串进行比较。

–u–-unique

不写入输入中重复的行。

–w, –-check-chars=chars

仅比较行中的 char 字符。

–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%

环境变量

有关影响 uniq 执行的以下环境变量的说明,请参见 environ(7):LANG、LC_ALL、LC_CTYPE、LC_MESSAGES 和 NLSPATH。

退出状态

将返回以下退出值:

0

成功完成。

>0

出现错误。

属性

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

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

另请参见

comm(1)ypcat(1)sort(1)uncompress(1)attributes(7)environ(7)standards(7)