消除重复的内容
只能用于已经排过序的数据输入,经常与sort命令结合起来使用
例如:
[root@localhost test]# cat file1.txt
bash
foss
hack
hack
[root@localhost test]# uniq file1.txt
bash
foss
hack
[root@localhost test]# sort file1.txt | uniq #去掉了重复行
bash
foss
hack
[root@localhost test]# uniq -u file1.txt -u, --unique:only print unique lines
bash
foss
[root@localhost test]# sort file1.txt | uniq -c -c , --count : 计数
1 bash
1 foss
2 hack
[root@localhost test]# sort file1.txt | uniq -d -d,repeated:重复了的
hack
uniq的-s,-w选项:
-s:指定可以跳 过前N个字符
-w:指定用于比较的最大字符数
例,只将每列的第三四个字母进行操作:
[root@localhost test]# cat data.txt
u:01:ha
d:01:lu
a:02:fu
c:02:bo
[root@localhost test]# sort data.txt | uniq -s 2 -w 2
a:02:fu
d:01:lu
uniq与xargs的结合使用
下面的命令会删除所有的文件,而这些文件的名字是从data.txt中读取出来的
uniq data.txt | xargs rm
$$:是当前运行的进程ID。
[root@localhost test]# echo $$
20967
[root@localhost test]# ps afx | grep 20967
20967 pts/3 Ss 0:00 | \_ -bash
21425 pts/3 S+ 0:00 | \_ grep 20967
本文详细介绍了Linux下uniq命令的使用方法及技巧,包括如何去除重复行、打印唯一行、计数及显示重复行等内容。通过实际例子展示了uniq命令与sort、xargs等命令的结合使用,帮助读者更好地理解和掌握uniq命令。

2943

被折叠的 条评论
为什么被折叠?



