
Linux 中的 uniq 命令主要用于识别和移除文本文件中重复的行,通常会配合 sort 命令一起使用。
uniq 可以用来查找文本文件中重复出现的行内容。
<pre class="brush:php;toolbar:false;">uniq [-cdu][-f][-s][-w][--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
以上就是linux去除重复行是什么-uniq 命令使用与实例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号