linux的sort 和 uniq 命令

一、sort 命令

二、uniq 命令

可以使用 uniq --h 查看帮助

注意:uniq 只会检测相邻的重复行,如果两行重复但它们位置不相邻,不能检测出来。因此 uniq 常与 sort 搭配使用。

-c  在每行前面显示重复次数

-d  显示重复行

-D  显示所有重复行

-u  仅显示出现一次的行

-i   忽略大小写

例子:cat test.txt   (例子内容参考博客https://www.zhukun.net/archives/7294

this is a test
this is a test
this is a test
i am tank
i love tank
i love tank
this is a test
whom have a try
WhoM have a try
you have a try
i want to abroad
those are good men
we are good men

uniq -c test.txt sort test.txt | uniq -c

3 this is a test
1 i am tank
2 i love tank
1 this is a test   //因为不相邻
1 whom have a try
1 WhoM have a try
1 you have a try
1 i want to abroad
1 those are good men
1 we are good men

1 i am tank
2 i love tank
1 i want to abroad
4 this is a test
1 those are good men
1 we are good men
1 whom have a try
1 WhoM have a try
1 you have a try

sort test.txt | uniq -d sort test.txt | uniq -D sort test.txt | uniq -u

i love tank
this is a test

i love tank
i love tank
this is a test
this is a test
this is a test
this is a test

i am tank
i want to abroad
those are good men
we are good men
whom have a try
WhoM have a try
you have a try

sort test.txt | uniq -ic

1 i am tank
2 i love tank
1 i want to abroad
4 this is a test
1 those are good men
1 we are good men
2 whom have a try   // 一个大写一个小写
1 you have a try

原文地址:https://www.cnblogs.com/min2day/p/13454228.html