亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 練習(xí)題(排序合并) [打印本頁]

作者: yestreenstars    時間: 2014-01-16 11:02
標(biāo)題: 練習(xí)題(排序合并)
騷年們,我又來出題了,這題跟昨天某人發(fā)的那題類似,屬于加強版。

處理前(亂序):
  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
復(fù)制代碼
處理后:
  1. apple: cat12,cat106-109,cat123,cat125
  2. pear: dog11,dog101,dog103-105,mouse106-109,mouse123,mouse125
復(fù)制代碼
處理要求:
1.根據(jù)第一列分類,對第二列進行合并,連續(xù)的用破折號相連,不連續(xù)的用逗號隔開,按照ACSII碼順序?qū)ψ宇悾ɡ鏿ear的dog和mouse)進行排序;
2.能用純awk或perl做出來最好。

以下是我的代碼,為了不影響大家的思路,我先隱藏起來。

作者: rdcwayx    時間: 2014-01-16 12:37
本帖最后由 rdcwayx 于 2014-01-16 12:38 編輯

回復(fù) 1# yestreenstars
回復(fù)了,但我也把代碼隱藏了。


   
作者: 這個冬天不冷    時間: 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. }
復(fù)制代碼
  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 ~]#
復(fù)制代碼

作者: 這個冬天不冷    時間: 2014-01-16 12:53
回復(fù) 1# yestreenstars
awk 處理 邏輯有點混亂,我繞過來,只好用php了,不要歧視我大php


   
作者: rdcwayx    時間: 2014-01-16 12:55
用php很好啊, 只要自己熟練,達到效果就可以了
作者: yestreenstars    時間: 2014-01-16 13:13
回復(fù) 4# 這個冬天不冷
不管是黑貓還是白貓,能抓到老鼠就是好貓

   
作者: yestreenstars    時間: 2014-01-16 13:46
回復(fù) 2# rdcwayx
這結(jié)果貌似不太符合要求啊,不過問題不大~
  1. apple,cat12,cat106-109,cat123,cat125
  2. pear,mouse106-109,mouse123,mouse125,dog11,dog101,dog103-105
復(fù)制代碼

作者: 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
復(fù)制代碼

作者: 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
學(xué)習(xí)。。學(xué)習(xí)
作者: 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=""
復(fù)制代碼
感覺python排序有點蛋疼,再研究下
作者: lxzkenney    時間: 2014-01-16 17:23
提示: 作者被禁止或刪除 內(nèi)容自動屏蔽
作者: yestreenstars    時間: 2014-01-16 17:27
回復(fù) 14# lxzkenney
你這結(jié)果就更達不到要求了{:2_169:}
   
作者: liyunxiang12    時間: 2014-01-16 17:39
我不太理解  m=gensub(/([^0-9]+).*/,"\\1",1,$2)  這個語句 能解釋下嗎 樓主?

講模式匹配到的 后向引用 給第一個  但是$2是做什么呢?
作者: yestreenstars    時間: 2014-01-16 17:47
回復(fù) 16# liyunxiang12

它的作用就是截取第二列的非數(shù)字部分

   
作者: reyleon    時間: 2014-01-16 19:27
我回復(fù)下能看到答案么? 懶得動了
作者: yinyuemi    時間: 2014-01-16 21:23
本帖最后由 yinyuemi 于 2014-01-16 21:29 編輯

回復(fù) 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

復(fù)制代碼

作者: yinyuemi    時間: 2014-01-16 21:24
gawk 4.0                    
作者: yestreenstars    時間: 2014-01-16 23:14
回復(fù) 19# yinyuemi

看起來好復(fù)雜,學(xué)習(xí)了match截取字符串的方法和4.0多維數(shù)組的處理~{: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
復(fù)制代碼

作者: yestreenstars    時間: 2014-01-17 00:01
回復(fù) 19# yinyuemi

學(xué)以致用,我也來個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
復(fù)制代碼
多行腳本有助于理解:
  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. }
復(fù)制代碼

作者: vic260844    時間: 2014-01-19 19:45
學(xué)習(xí)學(xué)習(xí)學(xué)習(xí)學(xué)習(xí)學(xué)習(xí)學(xué)習(xí)學(xué)習(xí)學(xué)習(xí)
作者: reb00t    時間: 2014-01-19 22:06
必須頂頂~~~
作者: 張不凡    時間: 2014-01-19 22:22
回復(fù)看答案~~!
作者: 戒愛丿小壞蛋    時間: 2014-01-20 12:05
新手學(xué)習(xí)路過!。。。。。!
作者: pitonas    時間: 2014-01-20 13:53
新手學(xué)習(xí),

我回復(fù)下能看到答案么?  {:2_172:}
作者: lklkxcxc    時間: 2014-01-20 16:29
頂頂頂頂頂頂頂頂頂頂頂頂頂
作者: runintostar    時間: 2014-01-21 12:05
昨天問題看了有點疑惑想問LZ
關(guān)于第二列的排序是優(yōu)先字母排序還是優(yōu)先數(shù)字排序的部分?
,今天上午按照字母優(yōu)先做了下。
  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. }'
復(fù)制代碼

作者: yestreenstars    時間: 2014-01-21 12:09
回復(fù) 30# runintostar

我在要求里已經(jīng)寫得很清楚了:
按照ACSII碼順序?qū)ψ宇悾ɡ鏿ear的dog和mouse)進行排序

   
作者: runintostar    時間: 2014-01-21 12:12
yestreenstars 發(fā)表于 2014-01-21 12:09
回復(fù) 30# runintostar

我在要求里已經(jīng)寫得很清楚了:


Sorry,昨天第一次打開的時候貌似有關(guān)于數(shù)字部分的描述,是后來刪掉了么?
不好意思不好意思弄錯了
作者: wanzairen    時間: 2014-01-22 14:35
看看最精簡的答案
作者: skyyy90    時間: 2014-05-24 10:48
大神們討論,我們學(xué)習(xí)!
作者: expert1    時間: 2014-05-24 12:12
Ascii碼排序是什么意思,跟我blog有個問題差不多。
比如cat/mouse,是比較首字母嗎
作者: yestreenstars    時間: 2014-05-24 16:14
回復(fù) 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
復(fù)制代碼

作者: tgwz88    時間: 2014-05-26 13:48
我是來看答案的
作者: huang6894    時間: 2014-08-25 08:34
翻翻老帖子
作者: ding_cw    時間: 2014-08-25 10:04
回復(fù)看一下
作者: 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
復(fù)制代碼

~~~條件判斷的終極考驗!
作者: wangqi6001    時間: 2014-08-26 16:33
我是來看答案的
作者: 行走的小布鞋    時間: 2014-08-26 17:16
來個方法吧,,想不出來
作者: liuchongs    時間: 2014-09-07 22:03
本帖最后由 liuchongs 于 2014-09-07 22:05 編輯

新手 剛學(xué)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
學(xué)習(xí)下  什么代碼
作者: KEN6503    時間: 2014-09-09 23:48
提示: 作者被禁止或刪除 內(nèi)容自動屏蔽
作者: 求上進    時間: 2014-09-10 09:43
沒有思路,求解...
作者: huangyu_945    時間: 2014-09-10 11:43
學(xué)習(xí)學(xué)習(xí)。。。。。。。。!
作者: zxcbvbbbbb    時間: 2015-05-13 15:11
提示: 作者被禁止或刪除 內(nèi)容自動屏蔽
作者: felixchen_sh    時間: 2015-05-13 15:28
看看思路。復(fù)雜的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$
復(fù)制代碼

作者: sky_eminem    時間: 2015-05-14 16:17
研究答案。。。。。。。。。。。。。。。。。。。
作者: moperyblue    時間: 2015-05-15 00:28
回復(fù) 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))))
復(fù)制代碼

作者: 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)  在嗎?

能詳細(xì)說一下你的 代碼的意思嗎?

------------------------------
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


==============可能這不是 本題目的答案, 但是現(xiàn)實生活中, 經(jīng)常遇到要 分組的情況啊================
作者: jason680    時間: 2015-05-18 13:53
回復(fù) 60# xuzhou2015


>> ... 但是現(xiàn)實生活中, 經(jīng)常遇到要 分組的情況啊..

How about the sort utility





歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2