文本处理时,经常要删除重复行,下面是三种方法
第一,用sort+uniq,注意,单纯uniq是不行的。
sort -n test.txt | uniq
第二,用sort+awk命令,注意,单纯awk同样不行,原因同上。sort -n $file | awk "{if($0!=line)print; line=$0}"
第三,用sort+sed命令,同样需要sort命令先排序。sort -n $file | sed "$!N; /^(.*)
1$/!P; D"
Shell脚本- # !/bin/sh
-
-
- file="test.txt"
-
- sort -n $file | uniq
-
- sort -n $file | awk "{if($0!=line)print; line=$0}"
-
- sort -n $file | sed "$!N; /^(.*)
1$/!P; D"
测试文件: yanggang@barry$ cat test.txt
aaa
bbbbb
ccccc
123
aaaaa
123
bbb
aaa
执行结果:yanggang@barry$ ./diffRow.sh
aaa
aaaaa
bbb
bbbbb
ccccc
123Ubuntu搭建Eclipse+JDK+SDK的AndroidLinux内核配置编译移植(基于x86)相关资讯 Linux教程
- Linux教程:如何在命令行中查看目 (07/28/2014 12:22:23)
- Linux 修改root密码 (11/03/2012 07:53:38)
- su - root 与su root的区别 (06/06/2012 00:39:40)
| - Linux进程间通信:消息队列 (01/28/2013 09:43:00)
- U盘安装Linux开机无法启动解决方法 (10/07/2012 08:55:52)
- Windows 7/Linux 同步时间 (05/15/2012 06:17:55)
|
本文评论 查看全部评论 (0)