免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1301 | 回复: 0
打印 上一主题 下一主题

重复行的删除 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-02 20:05 |只看该作者 |倒序浏览
要统计各种数据文件,若干记录是否在出现在,大日志文件里,或是jcl,统计某个记录的条数,连接shell,处理为原始的文本数据(从数据库来),操控数据库,shell调用sqlplus,执行sql,perl DBI连接oracle,自动建立目录,消除重复行,排序,等等,用awk,shell,sed,grep,perl乱七八糟的。
发现Perl单独就可以把上面的工作基本全都做了,只要你不嫌麻烦代码。Perl真的挺好玩了,特别是用Perl写的相对比较复杂的数据结构,还有OO
的东西。
    统计数据,要把一个文件里重复的记录删除,看了一眼网上给的答案,大体上就是,排序,之后用uniq,或awk
awk '{if ($0!=line) print;line=$0}' file
              
     一位达人用sed写的版本,如下:
          sed -f rmdup.sed yourfile
here is the rmdup.sed sed script:
            #n rmdup.sed - ReMove DUPlicate consecutive lines
             # read next line into pattern space (if not the last line)
$!N
# check if pattern space consists of two identical lines
s/^\(.*\)\n\1$/&/
# if yes, goto label RmLn, which will remove the first line in pattern space
t RmLn
# if not, print the first line (and remove it)
P
# garbage handling which simply deletes the first line in the pattern space
: RmLn
D
use `sort' first. there is no EFFICIENT way of sorting in sed/awk


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/60332/showart_724124.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP