linux uniq 命令
Linux 中的 uniq 命令主要用于识别和移除文本文件中重复的行,通常会配合 sort 命令一起使用。
uniq 可以用来查找文本文件中重复出现的行内容。
语法
<pre class="brush:php;toolbar:false">uniq [-cdu][-f][-s][-w][--help][--version][输入文件][输出文件]
参数说明:
- -c或–count 在每行前面显示该行重复的次数。
- -d或–repeated 只显示那些重复出现的行。
- -f或–skip-fields= 忽略指定栏位的比较。
- -s或–skip-Chars= 忽略指定数量字符的比较。
- -u或–unique 仅显示没有重复的行。
- -w或–check-chars= 设置要比较的字符数。
- –help 显示帮助信息。
- –version 查看版本号。
- [输入文件] 指定一个已经排序好的文本文件。如果不设置,则从标准输入读取;
- [输出文件] 指定结果输出的文件。如不指定,则输出到终端界面。
示例
假设在 testfile 文件中,第 2、3、5、6、7、9 行为相同内容,可以使用以下命令来去重:
<pre class="brush:php;toolbar:false">uniq testfile
testfile 的原始内容如下:
<pre class="brush:php;toolbar:false">$ cat testfile #原始内容 test 30 test 30 test 30 Hello 95 Hello 95 Hello 95 Hello 95 Linux 85 Linux 85
运行 uniq 命令后,输出结果为:
<pre class="brush:php;toolbar:false">$ uniq testfile #去重后的结果 test 30 Hello 95 Linux 85
若想查看每一行重复的次数,并在行前显示该次数,可使用以下命令:
<pre class="brush:php;toolbar:false">uniq -c testfile
输出如下:
<pre class="brush:php;toolbar:false">$ uniq -c testfile #显示重复次数 3 test 30 #表示该行出现了3次 4 Hello 95 #表示该行出现了4次 2 Linux 85 #表示该行出现了2次
需要注意的是,如果重复的行不是连续的,uniq 命令将无法正确识别,例如下面这种情况:
<pre class="brush:php;toolbar:false">$ cat testfile1 #原始内容 test 30 Hello 95 Linux 85 test 30 Hello 95 Linux 85 test 30 Hello 95 Linux 85
此时应先使用 sort 命令排序,再结合 uniq 命令处理:
<pre class="brush:php;toolbar:false">$ sort testfile1 | uniq Hello 95 Linux 85 test 30
统计各行在文件中的出现频率:
<pre class="brush:php;toolbar:false">$ sort testfile1 | uniq -c 3 Hello 95 3 Linux 85 3 test 30
查找文件中重复的行内容:
<pre class="brush:php;toolbar:false">$ sort testfile1 | uniq -d Hello 95 Linux 85 test 30
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END