uniq 检查及删除文本文件中重复出现的行列

作用

uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。

语法

uniq [-cdu][-f<栏位>][-s<字符位置>][-w<字符位置>][--help][--version][输入文件][输出文件]

参数

  • -c或--count 在每列旁边显示该行重复出现的次数。

  • -d或--repeated 仅显示重复出现的行列。

  • -f<栏位>或--skip-fields=<栏位> 忽略比较指定的栏位。

  • -s<字符位置>或--skip-chars=<字符位置> 忽略比较指定的字符。

  • -u或--unique 仅显示出一次的行列。

  • -w<字符位置>或--check-chars=<字符位置> 指定要比较的字符。

  • --help 显示帮助。

  • --version 显示版本信息。

  • [输入文件] 指定已排序好的文本文件。如果不指定此项,则从标准读取数据

  • [输出文件] 指定输出的文件。如果不指定此选项,则将内容显示到标准输出设备(显示终端)

示例

(1)使用 uniq 删除 demo 文件中相同的行,命令如下:

# 查看 demo 文件内容
hxstrive@localhost:~$ cat demo
See You again
See You again
It's been a long day without you my friend
And I'll tell you all about it when I see you again
We've come a long way from where we began
We've come a long way from where we began
Oh I'll tell you all about it when I see you again
When I see you again

# 删除 demo 文件的重复行
hxstrive@localhost:~$ uniq demo
See You again
It's been a long day without you my friend
And I'll tell you all about it when I see you again
We've come a long way from where we began
Oh I'll tell you all about it when I see you again
When I see you again

(2)检查 demo 文件并删除 demo 文件中重复出现的行,并在行首显示该行重复出现的次数。命令如下:

hxstrive@localhost:~$ uniq -c demo
      2 See You again  # 前面的数字 2 表示该行出现了 2 次
      1 It's been a long day without you my friend
      1 And I'll tell you all about it when I see you again
      2 We've come a long way from where we began
      1 Oh I'll tell you all about it when I see you again
      1 When I see you again

更多关于命令详细参考手册,请使用 man 命令或者 --help 参数获取帮助信息

关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号