免费注册 查看新帖 |

Chinaunix

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

perl或awk求解合并行问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-04 17:01 |只看该作者 |倒序浏览
输入文件:
cat  a.txt:
4056 78
4056 198
7717 98
7717 218
7718 98
7718 218
7719 98
7719 99
7719 218
7719 219
7720 98
7720 99
7720 218
7720 219
7720 220

输出:
4056   78,198
7717    98,218
7718    98,218
7719    98-99,218-219
7720    98-99,218-220


即:
第一列是索引号,第二列是一个数组,如果数组的值是连续的,应该用连字符来连接,如果不连续,需要用逗号来连接

感觉用perl实现方便一点,但是在数据存储上总有问题,应该是 hash-数组结构,求高人解答,AWK也可。

论坛徽章:
0
2 [报告]
发表于 2010-12-04 18:05 |只看该作者
自己写完了,但感觉很笨,尤其是数组存储那一块,其实我是想把hash的值直接指向一个数组,没成功,大侠们看看,有没有更灵巧一点的方法。
#!/usr/bin/perl
open(FO,ED_sw0 || die "cannot open file  \n";
while(<FO>
{
  chomp;
  if($_=~/^[0-9]/)
  {
   @data=split(/\s+/,$_);
   $current_shot=$data[0];
   $chan=$data[1];
   $dat=$data[1];
   $all{$current_shot}{$chan}=$dat;
   #print "$seq\n";
   #print  $current_line."\n";
  }
}
close(FO);


for  $mmm (sort {$a<=>$b} keys %all)
{
    print "shot is  ".$mmm."   ";
    @temp=();
    for $nnn (sort {$a<=>$b} keys %{$all{$mmm}})
    {
        #print  $all{$mmm}{$nnn}.",";      
        push (@temp, $all{$mmm}{$nnn});
    }
    for($i=0;$i<@temp;$i++)
    {
       if($i==0 && ($temp[$i+1] -$temp[$i]>1))
       {
          print $temp[$i].",";
       }
       elsif($i==0 &&  ($temp[$i+1] -$temp[$i]==1)  )
       {
          print  $temp[$i];
       }
       elsif($i!=(@temp-1) && ($temp[$i+1]-$temp[$i]>1) && ($temp[$i]-$temp[$i-1]==1)          )
       {
         print "-".$temp[$i].",";
       }
       elsif($i!=(@temp-1) && ($temp[$i+1]-$temp[$i]>1) && ($temp[$i]-$temp[$i-1]>1)          )
       {
          print $temp[$i].",";
       }
       elsif($i!=(@temp-1) && ($temp[$i+1]-$temp[$i]==1) && ($temp[$i]-$temp[$i-1]>1)          )
       {
         print $temp[$i];
       }
       elsif($i!=(@temp-1) && ($temp[$i+1]-$temp[$i]==1) && ($temp[$i]-$temp[$i-1]==1)          )
       {
           #not print
       }
       elsif($i==(@temp-1) && ($temp[$i]-$temp[$i-1]==1)          )
       {
          print "-".$temp[$i];
       }
       elsif($i==(@temp-1) && ($temp[$i]-$temp[$i-1]>1)          )
       {
          print $temp[$i];
       }
    }
    print  "\n";
}

论坛徽章:
0
3 [报告]
发表于 2010-12-04 20:08 |只看该作者
本帖最后由 rpbear 于 2010-12-04 20:12 编辑

我决定shell一下

  1. #! /bin/bash

  2. declare -i c
  3. declare -i d
  4. declare -i t
  5. declare -i n
  6. declare -i save1
  7. declare -i save2
  8. declare -i first

  9. echo 0 0 >> a.txt
  10. {
  11.         read a b
  12.         save1="$a"
  13.         save2="$b"
  14.         first="$b"
  15.         printf "%d  " $save1
  16.         while read c d;do
  17.                 let "t=d-1"
  18.                 if [ "$save1" -eq "$c" -a "$t" -eq "$save2" ];then
  19.                         save2="$d"
  20.                         continue
  21.                 elif [ "$save1" -ne "$c" ];then
  22.                         if [ "$first" -ne "$save2" ];then
  23.                                 printf " %d-%d"  $first $save2
  24.                         else
  25.                                 printf ",%d" $save2
  26.                         fi
  27.                         printf "\n%d  " $c
  28.                         save1="$c"
  29.                         save2="$d"
  30.                         first="$d"
  31.                         continue
  32.                 elif [ "$save1" -eq "$c" -a "$t" -ne "$save2" ];then
  33.                         if [ "$first" -ne "$save2" ];then
  34.                                 printf "%d-%d" $first $save2
  35.                         else
  36.                                 printf "%d" $save2
  37.                         fi
  38.                         first="$d"
  39.                         save2="$d"       
  40.                 fi                       
  41.         done
  42. } < a.txt >> b.txt
  43. n=`wc -l b.txt | cut -d " " -f1`
  44. n=$n-1
  45. head -n $n  b.txt
复制代码

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
4 [报告]
发表于 2010-12-04 22:19 |只看该作者
本帖最后由 cjaizss 于 2010-12-04 22:41 编辑

整个awk的

  1. #!/bin/gawk -f
  2. NF==2 {
  3.         a[$1]=a[$1]" "$2;
  4. }
  5. END {
  6.         for(i in a)
  7.                 b[i]=int(i);
  8.         x=asort(b);
  9.         for(i=1;i<=x;i++) {
  10.                 $0=a[b[i]];
  11.                 delete c;
  12.                 for(j=1;j<=NF;j++)
  13.                         c[j]=int($j);
  14.                 asort(c);
  15.                 printf("%d\t",b[i]);
  16.                 printf("%d",c[1]);
  17.                 start=c[1];
  18.                 cur=c[1];
  19.                 for(j=2;j<=NF;j++) {
  20.                         if(c[j]==cur+1) {
  21.                                 cur=c[j];
  22.                                 continue;
  23.                         }
  24.                         if(start!=cur)
  25.                                 printf("-%d",cur);
  26.                         printf(",%d",c[j]);
  27.                         start=cur=c[j];
  28.                 }
  29.                 if(start!=cur)
  30.                         printf("-%d",cur);
  31.                 printf("\n");
  32.         }
  33. }

复制代码
换了个变量名字,名字取的太傻了

  1. linux-0gt0:~ # cat 1
  2. 4056 78
  3. 4056 198
  4. 7717 98
  5. 7717 218
  6. 7718 98
  7. 7718 218
  8. 7719 98
  9. 7719 99
  10. 7719 218
  11. 7719 219
  12. 7720 98
  13. 7720 99
  14. 7720 218
  15. 7720 219
  16. 7720 220

  17. linux-0gt0:~ # ./1.awk 1
  18. 4056    78,198
  19. 7717    98,218
  20. 7718    98,218
  21. 7719    98-99,218-219
  22. 7720    98-99,218-220
复制代码

评分

参与人数 1可用积分 +10 收起 理由
expert1 + 10 NiuBility

查看全部评分

论坛徽章:
0
5 [报告]
发表于 2010-12-04 23:04 |只看该作者
本帖最后由 小鹭 于 2010-12-04 23:24 编辑

  1. #!/usr/bin/perl

  2. open(FILE,"a.txt") or die("$!");
  3. @unsorted=<FILE>;
  4. close(FILE);

  5. ### 先按照列1,列2排序, 然后得到hash数据
  6. @sorted = map {$_->[0]}
  7.           sort { $a->[1] <=> $b->[1]  or
  8.                  $a->[2] <=> $b->[2]
  9.                 }
  10.           map {[$_, (split(/\s+/))[0], (split(/\s+/))[1] ]} @unsorted;

  11. map {($a,$b)=split(/\s+/); $aggr{$a}.=$b . " "} @sorted;

  12. ### 重点是对组合的结果按照需求进一步处理

  13. foreach (keys %aggr) {
  14.     @out=split(/\s+/,$aggr{$_});
  15.     $str="";

  16.     for ($i=0; $i<scalar(@out); $i++) {
  17.        if ($out[$i+1] == $out[$i] + 1) {
  18.             $out[$i] .= "-";
  19.    
  20.        } else {
  21.            $out[$i].= ",";
  22.        }
  23.        $str .= $out[$i];
  24.     }
  25.    
  26.     @tmp=split(/,+/,$str);
  27.     map {$_=~s/-.*-/-/g;} @tmp;
  28.     $str=join(",",@tmp);

  29.     print "$_ $str\n";
  30. }
复制代码
4056    78,198
7717    98,218
7718    98,218
7719    98-99,218-219
7720    98-99,218-220

论坛徽章:
0
6 [报告]
发表于 2010-12-05 06:05 |只看该作者
本帖最后由 tangzongyao1f7c 于 2010-12-05 06:06 编辑

这里 sam 就是 a.txt
未优化。

awk '{print $1}' sam | sort -u | while read line; do
        grep "^$line" sam | awk '{print $2}' | sort -n | awk -v B=$line 'BEGIN {b=0}
                {       if(af==0)
                        {       b=$1
                                last=$1
                                af=1
                                printf("%d      ",B)
                        }
                        else if(af==1)
                        {       if($1 != last+1)
                                {        if(b==last)
                                                printf("%d, ",b)
                                        else    printf("%d-%d, ",b, last)
                                        b=$1
                                        last=$1
                                }
                                else    last=$1
                        }
                }END{   if(b==last)
                                printf("%d",b)
                        else    printf("%d-%d",b, last)
                        printf("\n")
                }'
done
exit

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
7 [报告]
发表于 2010-12-05 10:09 |只看该作者
  1. perl -lane 'chomp;if($F[0]!=$m){$s=~s/(?<=\d)-\d+(?=-)//g;print $s;$s=$_}else{if($F[1]==$n+1){$s.="-$F[1]"}else{$s.=",$F[1]"}};$m=$F[0];$n=$F[1];END{$s=~s/(?<=\d)-\d+(?=-)//g;print $s}' a.txt
复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
8 [报告]
发表于 2010-12-05 11:33 |只看该作者
练习一下perl
  1. #! /usr/bin/perl
  2. #pl028
  3. open(FILE , "lines007");
  4. chomp(@lines = <FILE>);
  5. for (@lines){
  6.   @c = split /\s+/;
  7.   if ($pre + 1 == $c[1]){
  8.     $hash{$c[0]} .= "-$c[1]";
  9.   }else{
  10.     $hash{$c[0]} .= ",$c[1]";
  11.   }
  12.   $pre = $c[1];
  13. }
  14. for $key (keys %hash) {
  15.   $hash{$key}=~s/-\d*-/-/g;
  16.   $hash{$key}=~s/^,//;
  17.   print "$key\t$hash{$key}\n";
  18. }
复制代码
./pl028
7718    98,218
7717    98,218
7720    98-99,218-220
4056    78,198
7719    98-99,218-219

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
9 [报告]
发表于 2010-12-05 13:03 |只看该作者
回复 9# 昭襄王

$hash{$key}=~s/-\d*-/-/g;
这个有点问题,把连续的数字多加几个试试看:

7720 218
7720 219
7720 220
7720 221
7720 222
7720 223

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
10 [报告]
发表于 2010-12-05 13:10 |只看该作者
回复 11# ly5066113


    还是TIM兄想的周全,改了一下
$hash{$key}=~s/-[0-9\-]*-/-/g;
  1. #! /usr/bin/perl
  2. #pl028
  3. open(FILE , "lines007");
  4. chomp(@lines = <FILE>);
  5. for (@lines){
  6.   @c = split /\s+/;
  7.   if ($pre + 1 == $c[1]){
  8.     $hash{$c[0]} .= "-$c[1]";
  9.   }else{
  10.     $hash{$c[0]} .= ",$c[1]";
  11.   }
  12.   $pre = $c[1];
  13. }
  14. for $key (keys %hash) {
  15.   $hash{$key}=~s/-[0-9\-]*-/-/g;
  16.   $hash{$key}=~s/^,//;
  17.   print "$key\t$hash{$key}\n";
  18. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP