免费注册 查看新帖 |

Chinaunix

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

重复数字之间距离 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-08-23 22:42 |只看该作者 |倒序浏览
RT
cat file01

  1. 121
  2. 230
  3. 444
  4. 142
  5. 567
  6. 908
  7. 121
  8. 132
  9. 211
  10. 320
  11. 142
  12. 121
  13. 901
  14. 239
  15. 121
复制代码
for example : 121  
the first 121 between the second (121) has 6 numbers
the second 121 between the third has 4 numbers
the third 121 between the forth has 2 numbers

how to write a shell script to get the correct result !
thanks

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
2 [报告]
发表于 2010-08-23 22:50 |只看该作者
上看下看左看右看,6是从哪里冒出来的?

论坛徽章:
0
3 [报告]
发表于 2010-08-23 22:55 |只看该作者
hehe, the first 121 between the second (121) has 5 numbers

论坛徽章:
0
4 [报告]
发表于 2010-08-23 22:56 |只看该作者
  1. #!/bin/awk -f
  2. {temp=$1;
  3.   num=NR;
  4.   for(i in a)
  5.     if(a[i]==temp){
  6.        print $1,num-i-1;
  7.        delete a[i];
  8.     }
  9.        a[num]=$1;
  10. }
复制代码
not good!

论坛徽章:
0
5 [报告]
发表于 2010-08-23 22:57 |只看该作者

  1. 121 5
  2. 142 6
  3. 121 4
  4. 121 2
复制代码

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
6 [报告]
发表于 2010-08-23 22:58 |只看该作者
回复 2# Shell_HAT


    多干几次就干来了,应该是行号差。

awk可以解决吧。

论坛徽章:
0
7 [报告]
发表于 2010-08-23 22:59 |只看该作者
本帖最后由 ywlscpl 于 2010-08-23 23:06 编辑
  1. awk -v RS=121 'RS==RT&&NR>1{print "The "++n" between "n+1" ("RS") has "NF" numbers"}' file
复制代码

论坛徽章:
0
8 [报告]
发表于 2010-08-23 23:08 |只看该作者
回复 7# ywlscpl


    good idea RS=121 !
  but I want to find all repeat data not only 121 !
  In fact , I consider the output like this,
121 :  5,4,2
142: 6

论坛徽章:
0
9 [报告]
发表于 2010-08-23 23:14 |只看该作者
回复 8# addictlinux
  1. for n in `awk 'a[$1]++==1' file`
  2. do
  3.    awk -v RS="$n" 'RS==RT&&NR>1{print "The "++n" between "n+1" ("RS") has "NF" numbers"}' file
  4.    echo "-----------------------"
  5. done
复制代码

论坛徽章:
0
10 [报告]
发表于 2010-08-23 23:22 |只看该作者
本帖最后由 ywlscpl 于 2010-08-23 23:28 编辑

another output type
  1. for n in `awk 'a[$1]++==1' file`
  2. do
  3.    awk -v RS="$n" 'RS==RT&&NR>1{printf !m++?RS": "NF:","NF}END{print ""}' file
  4. done
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP