天下事有难易乎?为之,则难者亦易矣;不为,则易者亦难矣。

Linux常用去除重复数据口令

itzoo 2018次浏览 0个评论

一、uniq干什么用的

文本中的重复行,基本上不是我们所要的,所以就要去除掉。linux下有其他命令可以去除重复行,但是我觉得uniq还是比较方便的一个。使用uniq的时候要注意以下二点

1,对文本操作时,它一般会和sort命令进行组合使用,因为uniq 不会检查重复的行,除非它们是相邻的行。如果您想先对输入排序,使用sort -u。

2,对文本操作时,若域中为先空字符(通常包括空格以及制表符),然后非空字符,域中字符前的空字符将被跳过

二、uniq参数说明

[root@iZ283syhkr8Z ~]# uniq --help 
Usage: uniq [OPTION]... [INPUT [OUTPUT]]
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'.

Report uniq bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'uniq invocation'

三、用一个测试文本查看每个命令的作用

[root@iZ283syhkr8Z lianxi]# cat itzoo-test.txt 
itzoo
itzoo
relyun
abc
classname
ab
relyun
lx
love
Relyun
AB
  • -c 用来统计相邻两行出现的次数,通常会和sort结合来使用
[root@iZ283syhkr8Z lianxi]# uniq -c itzoo-test.txt 
 2 itzoo
 1 relyun
 1 abc
 1 classname
 1 ab
 1 relyun
 1 lx
 1 love
 1 Relyun
 1 AB
[root@iZ283syhkr8Z lianxi]# sort itzoo-test.txt | uniq -c
 1 ab
 1 AB
 1 abc
 1 classname
 2 itzoo
 1 love
 1 lx
 2 relyun
 1 Relyun
  • -d 用来输出重复的行 也是只输出相邻两行重复的,可以结合sort来使用,输出全文重复的行
[root@iZ283syhkr8Z lianxi]# uniq -d itzoo-test.txt 
itzoo
[root@iZ283syhkr8Z lianxi]# sort itzoo-test.txt | uniq -d
itzoo
relyun
[root@iZ283syhkr8Z lianxi]# sort itzoo-test.txt | uniq -d -c
2 itzoo
2 relyun
  • -D 用来输出重复的行 也是只输出相邻两行重复的,和d不同的是,D会输出信息相同的行,可以结合sort来使用,输出全文重复的行。而且不能和-c使用
[root@iZ283syhkr8Z lianxi]# uniq -D itzoo-test.txt 
itzoo
itzoo
[root@iZ283syhkr8Z lianxi]# sort itzoo-test.txt | uniq -D
itzoo
itzoo
relyun
relyun
  • -i 忽略字母大小写
[root@iZ283syhkr8Z lianxi]# sort itzoo-test.txt | uniq -c 
1 ab
1 AB
1 abc
1 classname
2 itzoo
1 love
1 lx
2 relyun
1 Relyun
[root@iZ283syhkr8Z lianxi]# sort itzoo-test.txt | uniq -i -c
2 ab
1 abc
1 classname
2 itzoo
1 love
1 lx
3 relyun

其他的-u去重重复行并显示,-w不对比每行的前几个字符,如:uniq -w 5 itzoo-test.txt每行前5个字符不处理,-s忽略每行后几个字符,可以自己测试一下。


ITZOO版权所有丨如未注明 , 均为原创丨转载请注明来自IT乐园 ->Linux常用去除重复数据口令
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址