linux之去重uniq命令详解
linux之去重uniq命令详解
uniq是对文本文件进行行去去重的工具。
- 以行为单位,进行行与行之间的字符串比较并进行去重
- 只能对有序的文本行进行有效去重,所以常与sort命令结合使用
参数 | 解释 |
---|---|
-c | 统计行出现的次数 |
-d | 只显示重复的行并且去重 |
-u | 只显示唯一的行 |
-i | 忽略字母大小写 |
-f | 忽略前N个字段(字段间用空白字符分隔) |
1.与sort结合使用
准备文件
[root@linuxforliuhj test]# cat hello.sh
hello this is linux
be better
be better
i am lhj
hello this is linux
i am lhj
i am lhj
be better
i am lhj
have a nice day
have a nice day
hello this is linux
hello this is linux
have a nice day
zzzzzzzzzzzzzz
dddddddd
gggggggggggggggggggg
[root@linuxforliuhj test]#
【1】单独使用uniq命令
[root@linuxforliuhj test]# uniq hello.sh
hello this is linux
be better
i am lhj
hello this is linux
i am lhj
be better
i am lhj
have a nice day
hello this is linux
have a nice day
zzzzzzzzzzzzzz
dddddddd
gggggggggggggggggggg
[root@linuxforliuhj test]#
可以看出单独使用uniq命令时只对相邻重复行进行去重,无法进行有效去重
【2】与sort结合使用
[root@linuxforliuhj test]# sort hello.sh | uniq
be better
dddddddd
gggggggggggggggggggg
have a nice day
hello this is linux
i am lhj
zzzzzzzzzzzzzz
[root@linuxforliuhj test]#
先排序使重复的行相邻,然后使用uniq可以有效去重
【3】uniq支持管道符
[root@linuxforliuhj test]# cat hello.sh | sort | uniq
be better
dddddddd
gggggggggggggggggggg
have a nice day
hello this is linux
i am lhj
zzzzzzzzzzzzzz
[root@linuxforliuhj test]#
上次写sort那篇文章时说到sort支持管道符,同样uniq也一样支持管道符
2.统计出现的次数,使用-c参数
使用-c,–count可以统计重复行出现的次数
[root@linuxforliuhj test]# cat hello.sh | sort | uniq -c3 be better1 dddddddd1 gggggggggggggggggggg3 have a nice day4 hello this is linux4 i am lhj1 zzzzzzzzzzzzzz
[root@linuxforliuhj test]#
3.只显示重复的行并且去重,使用-d参数
使用-d,–repeated只显示重复的行并且重复的行只显示一次
[root@linuxforliuhj test]# cat hello.sh | sort | uniq -dc3 be better3 have a nice day4 hello this is linux4 i am lhj
[root@linuxforliuhj test]#
可以看到末尾的三行不重复的行没有显示,将重复的行进行去重后显示
3.只显示唯一的行,使用-u参数
[root@linuxforliuhj test]# cat hello.sh | sort | uniq -u
dddddddd
gggggggggggggggggggg
zzzzzzzzzzzzzz
[root@linuxforliuhj test]#
只有末尾三行是唯一的,所以只显示末尾的三行文本
4.忽略字母大小写,使用-i参数
[root@linuxforliuhj test]# cat test.txt
I am LHJ
i am lhj
HELLO this Is Zhang
hello this is zhang
[root@linuxforliuhj test]# cat test.txt | sort | uniq -i
hello this is zhang
i am lhj
[root@linuxforliuhj test]#
5.忽略前N个字段进行去重,使用-f参数
[root@linuxforliuhj test]# cat test.txt
1 I am LHJ
2 i am lhj
3 HELLO this Is Zhang
4 hello this is zhang
[root@linuxforliuhj test]# cat test.txt | sort | uniq -i -f 1
1 I am LHJ
3 HELLO this Is Zhang
[root@linuxforliuhj test]#
test.txt文件中每一行之前有行号,无法使用sort和uniq进行去重,可以使用-f参数忽略每一行的第一个字段,这样就可以忽略每一行之前的行号,对每一行之后的内容进行去重处理。
注意:字段之间必须是空白字符(空格或者tap键均属于空白字符)分隔,使用其他字符无法识别
对于这种情况也可以使用awk命令工具进行处理去除第一列的行号,然后通过管道符丢给sort和uniq命令处理,后续会继续更新awk等重要的文本处理工具。
linux之去重uniq命令详解相关推荐
- Linux 去重uniq命令详解
linux之去重uniq命令详解 uniq是对文本文件进行行去去重的工具. 以行为单位,进行行与行之间的字符串比较并进行去重 只能对有序的文本行进行有效去重,所以常与sort命令结合使用 参数 解释 ...
- linux cut命令学习,Linux中的cut 命令详解
今天小编要跟大家分享的文章是关于Linux中的cut 命令详解.cut 命令在Linux和Unix中的作用是从文件中的每一行中截取出一些部分,并输出到标准输出中.我们可以使用 cut 命令从一行字符串 ...
- Ubuntu (Linux) 系统 find 命令详解
Ubuntu (Linux) 系统 find 命令详解 在Ubuntu (Linux)系统的终端上,虽然没有像 windows 上那样简介易用的图形界面 搜索工具,但只要你使用足够熟练, 有一个强大命 ...
- linux ps(process status) 命令详解
linux ps(process status) 命令详解 功能说明:报告程序状况. 语 法:ps [-aAcdefHjlmNVwy][acefghLnrsSTuvxX][-C <指令名称> ...
- Linux操作系统上lsof命令详解
Linux操作系统上lsof命令详解 2011-10-08 18:31:31 http://xjsunjie.blog.51cto.com/999372/682865 标签:Linux lsof命令 ...
- linux slocate(secure locate) 命令详解
linux slocate(secure locate) 命令详解 功能说明:查找文件或目录. 语 法:slocate [-u][--help][--version][-d <目录>][查 ...
- Linux上的free命令详解
Linux上的free命令详解 转自: http://www.cnblogs.com/coldplayerest/archive/2010/02/20/1669949.html 解释一下Linux上f ...
- linux磁盘分区fdisk命令详解及云硬盘挂载实操
linux磁盘分区fdisk命令详解 1.什么是分区? 分区是将一个硬盘驱动器分成若干个逻辑驱动器,分区是把硬盘连续的区块当做一个独立的磁硬使用.分区表是一个硬盘分区的索引,分区的信息都会写进分区表. ...
- LINUX经常使用的命令详解
LINUX经常使用的命令详解 源地址:http://blog.itpub.net/29065182/viewspace-1189162/ 1.man 对你熟悉或不熟悉的命令提供帮助解释 eg:man ...
最新文章
- win2003服务器定时自动重启命令[计划任务]
- Redis的常用命令——hash的常用命令
- UI自学宝典,完整UI学习线路图
- 为什么知乎页面按Ctrl C 会自动粘贴一部份文字出来?
- html文本设置float,css怎么float(浮动)?
- Xcode做简易计算器
- 【数据结构与算法】布谷鸟散列表的Java实现
- Android camera(4)---Android Camera开发之基础知识篇
- 【干货】史上最全个性化推荐技术资料包(附50余份技术文档下载链接)
- 设置窗体的可见性无效
- 广义注意力- saliency map 关注图、gaze、Att
- 华为云计算HCIE学习笔记-FusionCompute
- 同一个项目中让R和Python无缝工作的五种方法
- 只要五分钟,即可将博客搬到公众号并完美布局(原创-独家)!
- Python 小白实例(一)——体脂率的计算(输入输出)
- 一战托福5个月112分 经验分享 + 备考资料大放送
- jpg格式电脑怎么弄_(常见问题)如何把HEIC格式图片转换成JPG格式?
- 微信开发——加密认证
- 微软账户无法登录(应用商店进不去等)
- Java将PDF转为图片Jpg
热门文章
- 宾馆客房管理系统正文
- pandas-groupby分组操作
- udp数据报从网卡驱动到用户空间流程总结
- Determine what tasks are periodically taking processor time
- 5. 字符串替换问题(golang)
- 【Unity3D插件】EasyTouch插件分享《手机摇杆插件》
- 使用runjs在博客园中显示demo效果图
- 【技术贴】合上笔记本屏幕盖子,再打开蓝屏自动重启,ntoskrnl.exe
- GNN algorithm(4): HAN, Heterogeneous Graph Attention Network
- CSS篇 累了,毁灭吧