uniq命令总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uniq命令总结相关的知识,希望对你有一定的参考价值。
uniq:通过消除重复内容,从给定输入中(stdin或命令行参数文件)找到单一的行。它也可以用户找出输入中出现的重复行。uniq只能用于排过序的数据输入,因此,uniq要么使用管道,要么将排序过的文件作为输入,并总是以这种方式与sort命令结合起来使用。
[[email protected] ~]# man uniq UNIQ(1) User Commands UNIQ(1) NAME uniq - report or omit repeated lines SYNOPSIS uniq [OPTION]... [INPUT [OUTPUT]] DESCRIPTION Filter adjacent matching lines from INPUT (or standard input), writing to OUTPUT (or standard output). With no options, matching lines are merged to the first occurrence. Mandatory arguments to long options are mandatory for short options too. -c, --count prefix lines by the number of occurrences -d, --repeated only print duplicate lines -D, --all-repeated[=delimit-method] print all duplicate lines delimit-method={none(default),prepend,separate} Delimiting is done with blank lines. -f, --skip-fields=N avoid comparing the first N fields -i, --ignore-case ignore differences in case when comparing -s, --skip-chars=N avoid comparing the first N characters -u, --unique only print unique lines -z, --zero-terminated end lines with 0 byte, not newline -w, --check-chars=N compare no more than N characters in lines --help display this help and exit --version output version information and exit A field is a run of blanks (usually spaces and/or TABs), then non-blank characters. Fields are skipped before chars. Note: ’uniq’ does not detect repeated lines unless they are adjacent. You may want to sort the input first, or use ‘sort -u’ without ‘uniq’. Also, comparisons honor the rules specified by ‘LC_COLLATE’.
1、-c参数:统计行出现的次数,并将次数放在行首。
[[email protected] ~]# cat test.txt test liyao oldboy test just oldboy test [[email protected] ~]# uniq -c test.txt 1 test 1 liyao 1 oldboy 1 test 1 just 1 oldboy 1 test [[email protected] ~]# sort test.txt|uniq -c 1 just 1 liyao 2 oldboy 3 test [[email protected] ~]#
通过对比可以发现,没有排序的输入,只是在每行前面显示前缀数字而已。
2、-d参数:只显示重复的行。
[[email protected] ~]# sort test.txt |uniq -d oldboy test [[email protected] ~]#
同样,对于无序输入,uniq达不到你想要的结果
[[email protected] ~]# uniq -d test.txt [[email protected] ~]#
3、-u参数:只显示唯一的行。
[[email protected] ~]# uniq -u test.txt test liyao oldboy test just oldboy test [[email protected] ~]# sort test.txt |uniq -u just liyao [[email protected] ~]#
uniq较常用的参数主要是这三个参数,熟练掌握这三个参数即可。其他,可以再做了解。
本文出自 “攸心斋” 博客,请务必保留此出处http://mofei.blog.51cto.com/6840705/1760730
以上是关于uniq命令总结的主要内容,如果未能解决你的问题,请参考以下文章