七月 9th, 2009uniq用法解释zt
uniq对指定的ASCII文件或标准输入进行唯一性检查,以判断文本文件中重复出现的行。常用于系统排查及日志分析。
一、版本
以红旗DC Server 5.0为例,自带版本为:
# uniq --version uniq (coreutils) 5.2.1 |
二、常用参数
命令格式:
uniq [options] [file1 [file2] ] |
uniq从已经排序好的文本文件file1中删除重复的行,输出到标注输出或file2。常作为过滤器,配合管道使用。
在使用uniq命令前,必须确保操作的文本文件已经过sort排序。若不带参数运行uniq,将删除重复的行。
常见参数有:
-c, --count 在每行旁边显示该行重复出现的次数 -d, --repeated 仅显示重复出现的行 -D, --all-repeated[=method] 以指定的格式打印所有重复的行。格式仅适用于长选项参数,可使用none(默认),prepend,separate三个,效果见后面。以空行进行区分。 -f n, --skip-fields=n 前n个字段与每个字段前的空白一起被忽略。一个字段(fields)是一个非空格、非制表符的字符串,彼此由制表符和空格隔开(字段从1开始编号)。与-n相同,n为字段数。 -i, --ignore-case 在判断重复行时,忽略大小写区别 -s n, --skip-chars=n 前n个字符被忽略,之前的字符被跳过(字符从1开始编号)。与+n相同,n为字符数。 -u, --unique 仅显示出现一次的行 -w n, --check-chars=n 仅比较每行前面n个字符 --help 显示帮助信息 --version 显示版本信息 [输入文件] 已经排序好的文本文件或标准输入 [输出文件] 指定的输出文件,若不指定,则显示在屏幕上 |
三、示例
1、原始文件
# cat text The year. The year. Second Second Second Third Third Four |
2、-D参数结果
# uniq -D text The year. The year. Second Second Second Third Third # uniq --all-repeated=prepend text The year. The year. Second Second Second Third Third # uniq --all-repeated=separate text The year. The year. Second Second Second Third Third |
3、-f和-s参数
原文:
# cat text The year. The year ok. Second Second Second Third Third Four |
参数结果:
# uniq -u text The year. The year ok. Four # uniq -u -f 1 text The year. The year ok. # uniq -u -f 2 text The year. The year ok. # uniq -u -f 3 text |
下面显示文件text中不重复的行,忽略前2个字段及后面的3个字符,即从第3个字段的第4个字符开始做比较:
(因为ok.后面还有\n回车符,当同第一行比较时,是有差异的)
# uniq -u -f 2 -s 3 text The year. The year ok. # uniq -u -f 2 -s 4 text # uniq -u -2 +3 text The year. The year ok. # uniq -u -2 +4 text |
看多一个更简单的例子:
# cat text test one eest one # uniq -u text test one eest one # uniq -u -f 1 text # uniq -u -s 1 text |
※所以,字段数和字符数都是从1开始的。
4、其他
uniq list list.new |
把list文件删除重复的行后,输出到list.new中。(重复的行仅保留一行)
sort names | uniq -d |
查看names文件中那些行是重复的
四、参考资料
原文:
http://www.oreillynet.com/linux/cmd/cmd.csp?path=u/uniq