Chinaunix

标题: 练习题(排序合并) [打印本页]

作者: yestreenstars    时间: 2014-01-16 11:02
标题: 练习题(排序合并)
骚年们,我又来出题了,这题跟昨天某人发的那题类似,属于加强版。

处理前(乱序):
  1. pear    mouse107
  2. pear    mouse123
  3. pear    mouse109
  4. pear    mouse125
  5. apple   cat123
  6. pear    dog105
  7. pear    dog101
  8. apple   cat12
  9. pear    dog104
  10. apple   cat108
  11. pear    dog11
  12. apple   cat125
  13. apple   cat106
  14. pear    mouse108
  15. pear    mouse106
  16. apple   cat107
  17. pear    dog103
  18. apple   cat109
复制代码
处理后:
  1. apple: cat12,cat106-109,cat123,cat125
  2. pear: dog11,dog101,dog103-105,mouse106-109,mouse123,mouse125
复制代码
处理要求:
1.根据第一列分类,对第二列进行合并,连续的用破折号相连,不连续的用逗号隔开,按照ACSII码顺序对子类(例如pear的dog和mouse)进行排序;
2.能用纯awk或perl做出来最好。

以下是我的代码,为了不影响大家的思路,我先隐藏起来。

作者: rdcwayx    时间: 2014-01-16 12:37
本帖最后由 rdcwayx 于 2014-01-16 12:38 编辑

回复 1# yestreenstars
回复了,但我也把代码隐藏了。


   
作者: 这个冬天不冷    时间: 2014-01-16 12:51
  1. <?php
  2. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  3. function zuhe($key ,$array) {
  4.     sort($array);
  5.     $res='';
  6.     $len = count($array);
  7.     for($i=0;$i<$len;$i++)
  8.     {
  9.         if($i==0) {
  10.             $tmp=$str=$key.$array[$i];
  11.             continue;
  12.         }
  13.         if($i > 0) {
  14.             if($array[$i]==$array[$i-1]+1){
  15.                 $str=$tmp."-" . $array[$i];
  16.             }
  17.             if($array[$i]!=$array[$i-1]+1){
  18.                 $str=$str. "," .$key.$array[$i];
  19.                 $tmp = $str;
  20.             }
  21.         } } return $str; }
  22. $fp = fopen('test.txt', "r") or die("Unable to open file!");
  23. $res = array();
  24. $result ='';
  25. $num_result='';
  26. $reg='/[a-z]+/';
  27. $num_reg='/[0-9]+/';
  28. while(!feof($fp)) {
  29.     $line = fgets($fp, 1024);
  30.     $tmp = explode(" ",$line);
  31.     preg_match($reg,$tmp[count($tmp)-1],$result);
  32.     preg_match($num_reg,$tmp[count($tmp)-1],$num_result);
  33.     $res[$tmp['0']][$result['0']][] = $num_result['0'];
  34. }
  35. foreach ($res as $key => $v){
  36.     ksort($v);
  37.     $res1[$key]=$v;
  38. }
  39. foreach ($res1 as $key => $v) {
  40.     $k=1;
  41.     if($key=='')break;
  42.     echo $key.": ";
  43.     foreach ($v as $vk => $vv) {
  44.         if($k>1)echo ",";
  45.         echo zuhe($vk,$vv);
  46.         $k++;
  47.     }
  48.     echo "\n";
  49. }
复制代码
  1. [root@everIover ~]# cat test.txt
  2. pear    mouse107
  3. pear    mouse123
  4. pear    mouse109
  5. pear    mouse125
  6. apple   cat123
  7. pear    dog105
  8. pear    dog101
  9. apple   cat12
  10. pear    dog104
  11. apple   cat108
  12. pear    dog11
  13. apple   cat125
  14. apple   cat106
  15. pear    mouse108
  16. pear    mouse106
  17. apple   cat107
  18. pear    dog103
  19. apple   cat109
  20. [root@everIover ~]# php test2.php
  21. pear: dog11,dog101,dog103-105,mouse106-109,mouse123,mouse125
  22. apple: cat12,cat106-109,cat123,cat125
  23. [root@everIover ~]#
复制代码

作者: 这个冬天不冷    时间: 2014-01-16 12:53
回复 1# yestreenstars
awk 处理 逻辑有点混乱,我绕过来,只好用php了,不要歧视我大php


   
作者: rdcwayx    时间: 2014-01-16 12:55
用php很好啊, 只要自己熟练,达到效果就可以了
作者: yestreenstars    时间: 2014-01-16 13:13
回复 4# 这个冬天不冷
不管是黑猫还是白猫,能抓到老鼠就是好猫

   
作者: yestreenstars    时间: 2014-01-16 13:46
回复 2# rdcwayx
这结果貌似不太符合要求啊,不过问题不大~
  1. apple,cat12,cat106-109,cat123,cat125
  2. pear,mouse106-109,mouse123,mouse125,dog11,dog101,dog103-105
复制代码

作者: mcshell    时间: 2014-01-16 14:51
perl~~
  1. my $h;
  2. map{push @{$h->{$_->[0]}->{$_->[2]}},$_->[3]}sort{$a->[2]  cmp $b->[2]||$a->[3]<=>$b->[3]}map{[/(\w+)\s+(([a-z]+)(\d+))/]}<DATA>;
  3. map{print "$_:";my $n=$_;my @arr;map{my $tmp=join ",",@{$h->{$n}->{$_}};
  4.                              1 while($tmp =~ s#(-)?(?<!\d)(\d+),(\d+)(?(?{$3!=$2+1})(*F))#$1?"-$3":"$2-$3"#e);                       
  5.                               push @arr, "$_" . join ",$_",split/,/,"$tmp";
  6.                              }keys $h->{$n};print join ",",@arr;print "\n"}keys $h;
  7. __DATA__
  8. pear    mouse107
  9. pear    mouse123
  10. pear    mouse109
  11. pear    mouse125
  12. apple   cat123
  13. pear    dog105
  14. pear    dog101
  15. apple   cat12
  16. pear    dog104
  17. apple   cat108
  18. pear    dog11
  19. apple   cat125
  20. apple   cat106
  21. pear    mouse108
  22. pear    mouse106
  23. apple   cat107
  24. pear    dog103
  25. apple   cat109
复制代码

作者: 423497786    时间: 2014-01-16 15:58
顺序有问题,没办法了用asort排序后就这样,不过添加了去重的功能[code][root@localhost ~]# cat 1.txt
pear    mouse107
pear    mouse123
pear    mouse109
pear    mouse125
apple   cat123
pear    dog105
pear    dog101
apple   cat12
pear    dog104
apple   cat108
pear    dog11
apple   cat125
apple   cat106
pear    mouse108
pear    mouse106
apple   cat107
pear    dog103
apple   cat109
[root@localhost ~]# awk '{   
  a[NR]=$1FS$2
}
END{
  asort(a)
  for (i=1;i<=NR;i++) {
    split(a[i],b,FS)
    if (!c[b[1]])
      c[b[1]]=b[2]","
    else {
      count=split(c[b[1]],d,",")
      match(d[count-1],/a-z+/)
      old1=substr(d[count-1],RSTART,RLENGTH)
      match(b[2],/a-z+/)
      new1=substr(b[2],RSTART,RLENGTH)
      match(d[count-1],/[0-9]+$/)
      old2=substr(d[count-1],RSTART,RLENGTH)
      match(b[2],/[0-9]+/)
      new2=substr(b[2],RSTART,RLENGTH)      
      if (old1==new1 && (old2+1)==new2||old2==new2) {
        c[b[1]]=substr(c[b[1]],1,length(c[b[1]])-1)
        sub(/-[0-9]+$/,"",c[b[1]])
        c[b[1]]=c[b[1]]"-"new2","
      }   
      else
        c[b[1]]=c[b[1]]""b[2]","
    }  
  }  
  for (i in c)
    print i":"substr(c[i],1,length(c[i])-1)
}' 1.txt
apple:cat106-109,cat12,cat123,cat125
pear:dog101,dog103-105,dog11,mouse106-109,mouse123,mouse125
[root@localhost ~]# [/code]
作者: 423497786    时间: 2014-01-16 15:59
[code][root@localhost ~]# cat 1.txt
pear    mouse107
pear    mouse123
pear    mouse109
pear    mouse125
apple   cat123
pear    dog105
pear    dog101
apple   cat12
pear    dog104
apple   cat108
pear    dog11
apple   cat125
apple   cat106
pear    mouse108
pear    mouse106
apple   cat107
pear    dog103
apple   cat109
[root@localhost ~]# awk '{   
  a[NR]=$1FS$2
}
END{
  asort(a)
  for (i=1;i<=NR;i++) {
    split(a[i],b,FS)
    if (!c[b[1]])
      c[b[1]]=b[2]","
    else {
      count=split(c[b[1]],d,",")
      match(d[count-1],/a-z+/)
      old1=substr(d[count-1],RSTART,RLENGTH)
      match(b[2],/a-z+/)
      new1=substr(b[2],RSTART,RLENGTH)
      match(d[count-1],/[0-9]+$/)
      old2=substr(d[count-1],RSTART,RLENGTH)
      match(b[2],/[0-9]+/)
      new2=substr(b[2],RSTART,RLENGTH)      
      if (old1==new1 && (old2+1)==new2||old2==new2) {
        c[b[1]]=substr(c[b[1]],1,length(c[b[1]])-1)
        sub(/-[0-9]+$/,"",c[b[1]])
        c[b[1]]=c[b[1]]"-"new2","
      }   
      else
        c[b[1]]=c[b[1]]""b[2]","
    }  
  }  
  for (i in c)
    print i":"substr(c[i],1,length(c[i])-1)
}' 1.txt
apple:cat106-109,cat12,cat123,cat125
pear:dog101,dog103-105,dog11,mouse106-109,mouse123,mouse125
[root@localhost ~]# [/code]
作者: 423497786    时间: 2014-01-16 16:11
改了下可以了[code]
sed 's/[a-z ]*/& /' 1.txt|sort -k1,1 -k2,2 -k3,3n|awk '{
  a[NR]=$1FS$2$3
}
END{
  for (i=1;i<=NR;i++) {
    split(a[i],b,FS)
    if (!c[b[1]])
      c[b[1]]=b[2]","
    else {
      count=split(c[b[1]],d,",")
      match(d[count-1],/a-z+/)
      old1=substr(d[count-1],RSTART,RLENGTH)
      match(b[2],/a-z+/)
      new1=substr(b[2],RSTART,RLENGTH)
      match(d[count-1],/[0-9]+$/)
      old2=substr(d[count-1],RSTART,RLENGTH)
      match(b[2],/[0-9]+/)
      new2=substr(b[2],RSTART,RLENGTH)      
      if (old1==new1 && (old2+1)==new2||old2==new2) {
        c[b[1]]=substr(c[b[1]],1,length(c[b[1]])-1)
        sub(/-[0-9]+$/,"",c[b[1]])
        c[b[1]]=c[b[1]]"-"new2","
      }   
      else
        c[b[1]]=c[b[1]]""b[2]","
    }  
  }  
  for (i in c)
    print i":"substr(c[i],1,length(c[i])-1)
}'
[/code]
作者: liyunxiang12    时间: 2014-01-16 16:40
学习。。学习
作者: TasteOracle    时间: 2014-01-16 16:54
  1. # -*- coding:utf-8 -*-
  2. #filepath=D:\file
  3. import re
  4. l=[]
  5. d={}
  6. joinlist=[]
  7. T=""
  8. def ch2int(value):
  9.     return int(re.search("\d+",value).group())
  10. f=open(r"D:\file")
  11. lines=f.readlines()
  12. for line in lines:
  13.     if not d:
  14.         l.append(line.split()[1])
  15.         d[line.split()[0]]=l
  16.         l=[]
  17.     else:
  18.         if d.has_key(line.split()[0]):
  19.             d[line.split()[0]].append(line.split()[1])
  20.         else:
  21.             l.append(line.split()[1])
  22.             d[line.split()[0]]=l
  23.             l=[]
  24. for i in d:
  25.     x=sorted(d[i],key=ch2int)
  26.     for j in range(len(x)-1):
  27.         m=re.search("\d+",x[j]).group()
  28.         n=re.search("\D+",x[j]).group()
  29.         if x[j+1]==n+str(int(m)+1):
  30.             if x[j] not in joinlist:
  31.                 joinlist.append(x[j])
  32.             joinlist.append(x[j+1])
  33.             continue
  34.         if not joinlist:
  35.             if not T:
  36.                 T=x[j]+","
  37.             else:
  38.                 T=T+x[j]+","
  39.         else:
  40.             T=T+joinlist[0]+"-"+joinlist[-1]+","
  41.             joinlist=[]
  42.     print i+":"+T+x[-1]
  43.     T=""
复制代码
感觉python排序有点蛋疼,再研究下
作者: lxzkenney    时间: 2014-01-16 17:23
提示: 作者被禁止或删除 内容自动屏蔽
作者: yestreenstars    时间: 2014-01-16 17:27
回复 14# lxzkenney
你这结果就更达不到要求了{:2_169:}
   
作者: liyunxiang12    时间: 2014-01-16 17:39
我不太理解  m=gensub(/([^0-9]+).*/,"\\1",1,$2)  这个语句 能解释下吗 楼主?

讲模式匹配到的 后向引用 给第一个  但是$2是做什么呢?
作者: yestreenstars    时间: 2014-01-16 17:47
回复 16# liyunxiang12

它的作用就是截取第二列的非数字部分

   
作者: reyleon    时间: 2014-01-16 19:27
我回复下能看到答案么? 懒得动了
作者: yinyuemi    时间: 2014-01-16 21:23
本帖最后由 yinyuemi 于 2014-01-16 21:29 编辑

回复 1# yestreenstars
  1. awk '{match($2,/([^0-9]+)([0-9]+)/,a);b[$1][a[1]][a[2]]=a[2]}END{for(i in b){printf i":\t";for(j=1;j<=asorti(b[i],c);j++){printf j==1?"":",";for(k=1;k<=(l=asort(b[i][c[j]],d));k++){if(d[k]==d[k+1]-1){printf d[k-1]!=d[k]-1?c[j]d[k]"-":""}else{printf k==1?c[j]d[k]",":(d[k-1]==d[k]-1?d[k]:c[j]d[k])(k!=l?",":"")}}};print ""}}' file
  2. apple:  cat12,cat106-109,cat123,cat125
  3. pear:   dog11,dog101,dog103-105mouse106-109,mouse123,mouse125

复制代码

作者: yinyuemi    时间: 2014-01-16 21:24
gawk 4.0                    
作者: yestreenstars    时间: 2014-01-16 23:14
回复 19# yinyuemi

看起来好复杂,学习了match截取字符串的方法和4.0多维数组的处理~{:3_203:}
   
作者: elu_ligao    时间: 2014-01-16 23:39
  1. [redhat@localhost 0108]$ cat lj.awk
  2. #!/bin/awk

  3. {
  4.         if(!a[$1,$2]++)
  5.                 b[$1]=b[$1]?b[$1]" "$2:$2
  6.         c[$1,$2]=c[$1,$2]?c[$1,$2]" "$3:$3
  7. }
  8. END{
  9.         for(i in b){
  10.                 l=split(b[i],sb)
  11.                 printf i": ";
  12.                 for(j=1;j<=l;++j){
  13.                         li=split(c[i,sb[j]],sc)
  14.                         min=sc[1]
  15.                         for(k=1;k<li;++k){
  16.                                 if(sc[k]+1!=sc[k+1]){
  17.                                         printf sc[k]==min?sb[j]min",":sb[j]min"-"sc[k]","
  18.                                         min=sc[k+1]
  19.                                 }
  20.                         }
  21.                         printf sc[k]==min?sb[j]min:sb[j]min"-"sc[k]
  22.                         if(j<l) printf ","
  23.                 }
  24.                 print ""
  25.         }

  26. }
  27. [redhat@localhost 0108]$ awk '{$2=gensub(/([^0-9]+)([0-9]+)/,"\\1\t\\2",1,$2)}1' lianjie_jq | sort -k1,2 -k3n | awk -f lj.awk
  28. apple: cat12,cat106-109,cat123,cat125
  29. pear: dog11,dog101,dog103-105,mouse106-109,mouse123,mouse125
复制代码

作者: yestreenstars    时间: 2014-01-17 00:01
回复 19# yinyuemi

学以致用,我也来个4.0版的awk:
  1. $ awk '{match($2,/([^0-9]+)(.*)/,a);b[$1][a[1]][a[2]]=a[2]}END{for(i in b){printf i":\t";for(j=0;j++<asorti(b[i],c);){for(k=0;k++<asort(b[i][c[j]],d);)t=t?d[k]-d[k-1]==1?t"-"c[j]d[k]:t","c[j]d[k]:c[j]d[k];s=s?s","t:t;t=""}gsub(/-[^,]+-[^0-9]+/,"-",s);print s;s=""}}' i
  2. apple:  cat12,cat106-109,cat123,cat125
  3. pear:   dog11,dog101,dog103-105,mouse106-109,mouse123,mouse125
复制代码
多行脚本有助于理解:
  1. #!/bin/awk -f
  2. {
  3.         match($2,/([^0-9]+)(.*)/,a)
  4.         b[$1][a[1]][a[2]]=a[2]
  5. }
  6. END{
  7.         for(i in b){
  8.                 printf i":\t"
  9.                 for(j=0;j++<asorti(b[i],c);){
  10.                         for(k=0;k++<asort(b[i][c[j]],d);){
  11.                                 t=t?d[k]-d[k-1]==1?t"-"c[j]d[k]:t","c[j]d[k]:c[j]d[k]
  12.                         }
  13.                         s=s?s","t:t
  14.                         t=""
  15.                 }
  16.                 gsub(/-[^,]+-[^0-9]+/,"-",s)
  17.                 print s
  18.                 s=""
  19.         }
  20. }
复制代码

作者: vic260844    时间: 2014-01-19 19:45
学习学习学习学习学习学习学习学习
作者: reb00t    时间: 2014-01-19 22:06
必须顶顶~~~
作者: 张不凡    时间: 2014-01-19 22:22
回复看答案~~!
作者: 戒爱丿小坏蛋    时间: 2014-01-20 12:05
新手学习路过!!!!!!!!
作者: pitonas    时间: 2014-01-20 13:53
新手学习,

我回复下能看到答案么?  {:2_172:}
作者: lklkxcxc    时间: 2014-01-20 16:29
顶顶顶顶顶顶顶顶顶顶顶顶顶
作者: runintostar    时间: 2014-01-21 12:05
昨天问题看了有点疑惑想问LZ
关于第二列的排序是优先字母排序还是优先数字排序的部分?
,今天上午按照字母优先做了下。
  1. awk 'BEGIN{i=0;j=0};
  2. {a[$1,$2]=$2;b[$1]++};
  3. END{for(c in a)
  4. {
  5. s[c]=substr(a[c],1,match(a[c],/[0-9]*$/)-1);
  6. n[c]=substr(a[c],RSTART,RLENGTH);
  7. }
  8. for(d in b)
  9. {
  10. printf d": ";
  11. for(i=0;i<=b[d];i++)
  12. {
  13. first=0;
  14. for(c in s)
  15. {
  16. if(index(c,d)!=1){continue}
  17. if(m[c]==1){continue}
  18. if(first==0)
  19. {
  20. nows=s[c];
  21. nown=n[c];
  22. nowc=c;
  23. first=1;
  24. continue;
  25. }
  26. if(s[c]<=nows&&int(n[c])<=int(nown))
  27. {
  28. nows=s[c];
  29. nown=n[c];
  30. nowc=c;
  31. }
  32. }
  33. m[nowc]=1;
  34. if(i==0){lasts=nows;lastn=nown;count=0;continue}
  35. if(nows==lasts&&int(nown)==int(lastn+count+1)){count++;continue}
  36. if(count==0)printf lasts lastn;
  37. else printf lasts lastn"-"lastn+count;
  38. lasts=nows;
  39. lastn=nown;
  40. count=0;
  41. if(i!=b[d])printf ",";
  42. }
  43. printf "\n";
  44. }
  45. }'
复制代码

作者: yestreenstars    时间: 2014-01-21 12:09
回复 30# runintostar

我在要求里已经写得很清楚了:
按照ACSII码顺序对子类(例如pear的dog和mouse)进行排序

   
作者: runintostar    时间: 2014-01-21 12:12
yestreenstars 发表于 2014-01-21 12:09
回复 30# runintostar

我在要求里已经写得很清楚了:


Sorry,昨天第一次打开的时候貌似有关于数字部分的描述,是后来删掉了么?
不好意思不好意思弄错了
作者: wanzairen    时间: 2014-01-22 14:35
看看最精简的答案
作者: skyyy90    时间: 2014-05-24 10:48
大神们讨论,我们学习!
作者: expert1    时间: 2014-05-24 12:12
Ascii码排序是什么意思,跟我blog有个问题差不多。
比如cat/mouse,是比较首字母吗
作者: yestreenstars    时间: 2014-05-24 16:14
回复 35# expert1

对,如果第一个字母不同就比较第一个字母,第一个字母相同就比较第二个字母,依此类推……
   
作者: klainogn    时间: 2014-05-24 19:21
本帖最后由 klainogn 于 2014-05-24 20:21 编辑

我也来一个,只是排序上不太理想:

  1. lion@Lion:~$ cat test.awk
  2. {
  3.     a[$1]=a[$1]?a[$1]SUBSEP$2:$2
  4. }
  5. END{
  6.     for(i in a){
  7.         delete c
  8.         string=""
  9.         split(a[i], b, SUBSEP)
  10.         for(j=0; j++<asort(b); ){
  11.             match(b[j], /^(.*[^0-9])([0-9]+)$/, kv)
  12.             string=(kv[2]==last[kv[1]]+1)?string"-"kv[2]:string","b[j]
  13.             last[kv[1]]=kv[2]
  14.         }
  15.         sub(/^,/,"",string)
  16.         while(gsub(/-[0-9]+-/,"-", string)){}
  17.         print i":\t"string
  18.     }
  19. }
  20. lion@Lion:~$ awk -f test.awk test
  21. apple cat106-109,cat12,cat123,cat125
  22. pear mouse106-109,mouse123,mouse125,dog101,dog103-105,dog11
复制代码

作者: tgwz88    时间: 2014-05-26 13:48
我是来看答案的
作者: huang6894    时间: 2014-08-25 08:34
翻翻老帖子
作者: ding_cw    时间: 2014-08-25 10:04
回复看一下
作者: prcardin    时间: 2014-08-25 11:26
求看星辰大婶的代码
作者: prcardin    时间: 2014-08-25 11:30
求看星辰大婶的代码
作者: louis0o0    时间: 2014-08-25 15:16
看看答案                                 
作者: chengchow    时间: 2014-08-26 09:57
本帖最后由 chengchow 于 2014-08-26 10:08 编辑
  1. #!/bin/bash
  2. a=(`sort -n file | awk '{print $1}'`)
  3. b=(`sort -n file | awk '{print $2}' | grep -Po [^0-9]+`)
  4. c=(`sort -n file | grep -Po [0-9]+`)
  5. count=0

  6. for ((i=0;i<=${#a[*]};i++))
  7. do
  8.     if [[ ${a[$i]} != ${a[$(($i-1))]} ]];then
  9.         total="$total\n${a[$i]}\t${b[$i]}${c[$i]}"
  10.     else
  11.         if [[ ${b[$i]} = ${b[$(($i-1))]} ]];then
  12.             if [[ ${c[$i]} = $((${c[$(($i-1))]}+1)) ]];then
  13.                 let count=$count+1
  14.             else
  15.                 if [[ $count = 0 ]];then
  16.                     total="$total,${b[$i]}${c[$i]}"
  17.                 else
  18.                     total="$total-${c[$(($i-1))]},${b[$i]}${c[$i]}"
  19.                     count=0
  20.                 fi
  21.             fi
  22.         else
  23.                 total="$total,${b[$i]}${c[$i]}"
  24.          fi
  25.     fi
  26. done
  27. echo -e "$total"

  28. exit 0
复制代码

~~~条件判断的终极考验!
作者: wangqi6001    时间: 2014-08-26 16:33
我是来看答案的
作者: 行走的小布鞋    时间: 2014-08-26 17:16
来个方法吧,,想不出来
作者: liuchongs    时间: 2014-09-07 22:03
本帖最后由 liuchongs 于 2014-09-07 22:05 编辑

新手 刚学sed 硬凑的
cat filename | sort | sed -n /apple/p | sed '1s/apple */apple: /;:a;N;s/ *\napple */,/;$!ba' | sed 's/\(106\).*\(109\)/\1-\2/;s/\(cat106-109\),\(cat12\)/\2,\1/';cat filename | sort  | grep pear | sed '1s/pear */pear: /;:a;N;s/
\npear */,/;$!ba;s/\(103\).*\(105\)/\1-\2/;s/\(106\).*\(109\)/\1-\2/;s/\(dog101\)\(.*\)\(dog11\)/\3,\1\2/'

作者: wiliiwin    时间: 2014-09-08 10:48
学习下  什么代码
作者: KEN6503    时间: 2014-09-09 23:48
提示: 作者被禁止或删除 内容自动屏蔽
作者: 求上进    时间: 2014-09-10 09:43
没有思路,求解...
作者: huangyu_945    时间: 2014-09-10 11:43
学习学习!!!!!!!!!!
作者: zxcbvbbbbb    时间: 2015-05-13 15:11
提示: 作者被禁止或删除 内容自动屏蔽
作者: felixchen_sh    时间: 2015-05-13 15:28
看看思路。复杂的shell还是搞不定
作者: yaoliwei    时间: 2015-05-13 16:42
本帖最后由 yaoliwei 于 2015-05-13 17:20 编辑

不太对

  1. bash-3.2$ awk 'function printarr(fruit,array){printf fruit":\t";tn=asorti(array);for (i=0;i<tn;i++)printf array[i]" ";print}/pear/{p[$2]}/apple/{a[$2]}END{printarr("apple",a);printarr("pear",p)}' file1
  2. apple:   cat106 cat107 cat108 cat109 cat12 cat123
  3. pear:    dog101 dog103 dog104 dog105 dog11 mouse106 mouse107 mouse108 mouse109 mouse123
  4. bash-3.2$
复制代码

作者: sky_eminem    时间: 2015-05-14 16:17
研究答案。。。。。。。。。。。。。。。。。。。
作者: moperyblue    时间: 2015-05-15 00:28
回复 1# yestreenstars
python 版:
  1. #!/usr/bin/env python
  2. #encoding:utf-8

  3. import itertools
  4. import operator

  5. def getDataFromFile(filepath):
  6.     with open(filepath) as f:
  7.         for line in f:
  8.             col = line.strip().split()
  9.             yield (col[0],col[1])


  10. def getRanges(li):
  11.     if type(li[0]) == str:
  12.         li = map(int,li)
  13.     pos = [j -i for i,j in enumerate(li)]
  14.     t = 0
  15.     for k,gr in itertools.groupby(pos):
  16.         l = len(list(gr))
  17.         st_pos = li[t]
  18.         t += l
  19.         yield range(st_pos,st_pos + l)


  20. def getValueList(li):
  21.     ret = []

  22.     for k,gr in itertools.groupby(sorted(li),operator.itemgetter(0)):
  23.         g = list(gr)
  24.         values = [x[1] for x in g]
  25.         for x in getRanges(values):
  26.             if len(x)>1:
  27.                 ret.append(k+str(x[0])+"-"+str(x[-1]))
  28.             else:
  29.                 ret.append(k+str(x[0]))

  30.     return ret



  31. def getGroupbyWorld(li):
  32.     import re
  33.     for x in li:
  34.         yield re.findall(r'(\D+)(\d+)',x)[0]



  35. if __name__ == '__main__':
  36.     data = [x for x in getDataFromFile('1.txt')]
  37.     data.sort(key=operator.itemgetter(0))

  38.     import re

  39.     for k,gr in itertools.groupby(data,key=operator.itemgetter(0)):
  40.         g = list(gr)
  41.         values = [x[1] for x in g]
  42.         li = list(getGroupbyWorld(values))
  43.         print k+': '+','.join(sorted(getValueList(li),key=lambda x:int(re.match(r'\D+(\d+)',x).group(1))))
复制代码

作者: jason680    时间: 2015-05-15 11:11

awk only

$ awk 'function p(v){if(s=="-")printf("%s%s",s,D2 D3);printf("%s",v);D1=d[1];D2=d[2];D3=d[3]}{match($2,/([^0-9]+)([0-9]+)/,d);a[sprintf("%-15s%-15s%9d",$1,d[1],d[2])]=$0}END{t=asorti(a,b);for(n=1;n<=t;n++){split(b[n],d," +");if(d[1]!=D1){p(N d[1]": "d[2]d[3]);N="\n";s=",";continue};if(d[2]!=D2){p(","d[2]d[3]);continue};if(d[3]==D3+1){s="-";D3=d[3]}else{p(","d[2]d[3]);s=","}}}END{p("\n")}' FILE
apple: cat12,cat106-cat109,cat123,cat125
pear: dog11,dog101,dog103-dog105,mouse106-mouse109,mouse123,mouse125

作者: 聆雨淋夜    时间: 2015-05-15 23:02
看一下答案。
作者: ztyhua    时间: 2015-05-18 08:40
看一下看一下
作者: xuzhou2015    时间: 2015-05-18 13:38

lxzkenney (UID: 23464606)  在吗?

能详细说一下你的 代码的意思吗?

------------------------------
sort demo.txt \
|sed -r ':1
N
s/^(\S+)\s+(.+)\n\1\s+(.+)$/\1\t\2,\3/
t1
P
D'

-------------------------------

我没看明白啊?

-----------------说明-----------
我的意思是:
$ cat demo1|sort
apple   cat106
apple   cat107
apple   cat108
apple   cat109
apple   cat12
apple   cat123
apple   cat125
pear    dog101
pear    dog103
pear    dog104
pear    dog105
pear    dog11
pear    mouse106
pear    mouse107
pear    mouse108
pear    mouse109
pear    mouse123
pear    mouse125

是 怎么变成 下面的样子的???
apple   cat106,cat107,cat108,cat109,cat12,cat123,cat125
pear    dog101,dog103,dog104,dog105,dog11,mouse106,mouse107,mouse108,mouse109,mouse123,mouse125


==============可能这不是 本题目的答案, 但是现实生活中, 经常遇到要 分组的情况啊================
作者: jason680    时间: 2015-05-18 13:53
回复 60# xuzhou2015


>> ... 但是现实生活中, 经常遇到要 分组的情况啊..

How about the sort utility





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2