Chinaunix

标题: 帮忙合并两个文件中的不同内容 [打印本页]

作者: jiujiujiu338    时间: 2011-08-17 09:03
标题: 帮忙合并两个文件中的不同内容
本帖最后由 jiujiujiu338 于 2011-08-17 12:01 编辑

有两个文件,分别为file1.txt, file2.txt,合并为file3.txt其中
file1为:
begin
name=A1  No: 6,
weight=34.34 56.78
height=2+
date=1
No=6
11.105 81.8804
13.081 11.386
16.190 9.74307
163.085 8.32817
end

begin
name=A2  No: 3,
weight=35.34 55.78
height=1+
date=5
No=3
31.105 81.8804
13.081 11.386
16.190 9.74307
163.085 8.32817
end

file2为
begin
name=A1  No: 6,
weight=34.34 56.78
height=2+
date=1
No=6
32.105 82.8804
33.081 11.386
46.190 10.74307
263.085 8.32817
end

begin
name=A3  No: 3,
weight=35.34 55.78
height=5+
date=5
No=3,
11.105 81.8804
13.081 11.386
16.190 9.74307
163.085 8.32817
end

合并为file3、
begin
name=A1  
weight=34.34 56.78
height=2+
11.105 81.8804
13.081 11.386
16.190 9.74307
32.105 82.8804
33.081 11.386
46.190 10.74307

163.085 8.32817
263.085 8.32817
end

begin
name=A2  
weight=35.34 55.78
height=2+
31.105 81.8804
13.081 11.386
16.190 9.74307
163.085 8.32817
end

begin
name=A3  
weight=35.34 55.78
height=5+
11.105 81.8804
13.081 11.386
16.190 9.74307
163.085 8.32817
end

学习perl中遇到这样一个题,希望各位高手能够帮够给以帮助,即把file1和file2合并为file3,即把不同的数字合并,数字合在一起并按照第一列排序,即像上边例子中所示,多谢各位了,多谢多谢~
作者: jiujiujiu338    时间: 2011-08-17 10:11
希望各位帮帮忙
作者: zhlong8    时间: 2011-08-17 10:21
你这个很高科技啊,第一次来给你打个 8折好了。你要付 RMB 还是积分啊
作者: jiujiujiu338    时间: 2011-08-17 10:25
回复 3# zhlong8


    8折?这个怎么个价格呀?
作者: zhlong8    时间: 2011-08-17 10:32
回复  zhlong8


    8折?这个怎么个价格呀?
jiujiujiu338 发表于 2011-08-17 10:25



    你把需求写清楚了大家来竞价啊,我中标了给你打个8折
作者: jiujiujiu338    时间: 2011-08-17 10:52
回复 5# zhlong8


    ~~~~(>_<)~~~~ 现在没有积分,作为学生的我也没有钱怎么办啊,呜呜
作者: jiujiujiu338    时间: 2011-08-17 10:56
回复 5# zhlong8


    这位高手,您就帮帮忙吧,感激不尽啊
作者: jason680    时间: 2011-08-17 10:57
有两个文件,分别为file1.txt, file2.txt,合并为file3.txt其中
file1为:
begin
name=A1  No: 6,
weigh ...
jiujiujiu338 发表于 2011-08-17 09:03


lz 乱了序...
16.190 9.74307
163.085 8.32817
32.105 82.8804
33.081 11.386

作者: jiujiujiu338    时间: 2011-08-17 11:07
回复 8# jason680


    嗯呐,多谢多谢,改过来啦,还望各位路过的好人帮帮忙啊
作者: zhlong8    时间: 2011-08-17 11:12
回复  zhlong8


    这位高手,您就帮帮忙吧,感激不尽啊
jiujiujiu338 发表于 2011-08-17 10:56



    你不说清楚不好计费也没法做啊。两个文件多大,或者多少项?除了合并部分其它地方是不是严格匹配,包括等号后面的空格、No最后的逗号等等。
作者: yinyuemi    时间: 2011-08-17 11:34
本帖最后由 yinyuemi 于 2011-08-17 11:38 编辑

回复 1# jiujiujiu338
  1. #!/bin/perl
  2. use strict;

  3. open (my $fh1,"file1");
  4. open (my $fh2,"file2");
  5. open (my $fh3,">","file3");

  6. my %hash;
  7. my $t="";

  8. while (<$fh1>){
  9.         if($_!~/end/ and $_!~/^$/){
  10.                 if($_!~/^\d/){$t.=$_;}
  11.                 else{$hash{$t}.=$_ ;}
  12.                 }
  13.         else{$t="";
  14.                 }
  15. }
  16. while (<$fh2>){
  17.         if($_!~/end/ and $_!~/^$/){
  18.                 if($_!~/^\d/){$t.=$_;}
  19.                 else{$hash{$t}.=$_ ;}
  20.                 }
  21.         else{$t="";
  22.                 }
  23.         }

  24. foreach my $p (keys %hash) {
  25.         print  $fh3 $p, join "\n", (sort {$a<=>$b} (split /\n/, $hash{$p})),"end\n\n";
  26.         }

  27. close($fh1);
  28. close($fh2);
  29. close($fh3);
复制代码

作者: jiujiujiu338    时间: 2011-08-17 12:11
回复 11# yinyuemi


    多谢多谢,我好好看看呐
作者: jiujiujiu338    时间: 2011-08-17 15:03
本帖最后由 jiujiujiu338 于 2011-08-17 15:05 编辑

两个文件file1和file2合并为file3,大家帮忙看看怎么合并啊

file1为
word
word11
word12
word13
12.1 11
42.22 6.5
word0

file2为
word
word11
word12
word13
9.76 12
76.6 88
word0

合并为file3
word
word11
word12
word13
9.76 12
12.1 11
42.22 6.5
76.6 88
word0

数字合并,并且按照顺序,谢谢路过的各位了
作者: alabos    时间: 2011-08-17 20:38
  1. #!/usr/bin/perl

  2. use Tie::File;

  3. open my $fh,'>','file_3.txt' || die "$!\n";

  4. tie @file_1,'Tie::File','file_1.txt' || die "$!";
  5. tie @file_2,'Tie::File','file_2.txt' || die "$!";

  6. foreach ( 0 .. $#file_1)
  7. {
  8.     print "$_\t$file_1[$_]\n";

  9.     if ($file_1[$_] eq $file_2[$_])
  10.     {
  11.         print $fh $file_1[$_],"\n";
  12.     }
  13.     else
  14.     {
  15.         print $fh $file_1[$_],"\n";
  16.         print $fh $file_2[$_],"\n";
  17.     }
  18. }



复制代码
你要合并的2个文件的行数是一样的吧,不然也不会存在你说的相同的问题了,试试这个代码吧!

作者: zhlong8    时间: 2011-08-17 20:41
需求都没搞清楚呢就这么多抢生意的{:3_183:}
作者: jiujiujiu338    时间: 2011-08-17 21:21
回复 16# alabos


    多谢多谢啦,小妹刚学习perl不久,有点吃力,多谢帮助,比只看书强多了
作者: zhlong8    时间: 2011-08-17 21:29
回复  alabos


    多谢多谢啦,小妹刚学习perl不久,有点吃力,多谢帮助,比只看书强多了
jiujiujiu338 发表于 2011-08-17 21:21



    你不会是搞基因的吧 {:3_184:}
作者: jiujiujiu338    时间: 2011-08-17 21:37
回复 18# zhlong8


    算是吧,版主
作者: zhlong8    时间: 2011-08-17 21:39
回复  zhlong8


    算是吧,版主
jiujiujiu338 发表于 2011-08-17 21:37



    那你多介绍点美女来,这里很多弟兄愿意义务劳动的 {:3_187:}
作者: jiujiujiu338    时间: 2011-08-17 22:10
回复 20# zhlong8


    额......版主不能帮助一下嘛
作者: yinyuemi    时间: 2011-08-18 09:06
回复 22# jiujiujiu338


   假如我那段perl程序名叫s.pl,运行下面的代码(如果你是win,把单引号改成双引号),再执行试试

    perl -i -lpne 's/end/word0/g' s.pl
作者: jiujiujiu338    时间: 2011-08-18 10:05
回复  jiujiujiu338


   假如我那段perl程序名叫s.pl,运行下面的代码(如果你是win,把单引号改成双引 ...
yinyuemi 发表于 2011-08-18 09:06



    不好意思啊,之前写的程序运行时不成功,出来一连串的Argument "13.081 11.386" isn't numeric in numeric comparison (<=>) 不知怎么修改,刚您写的这句话没有看懂,希望能够写的明了,额太笨了
作者: yinyuemi    时间: 2011-08-18 11:36
回复 25# jiujiujiu338

  1.     #!/bin/perl
  2. use strict;

  3. open (my $fh1,"file1");
  4. open (my $fh2,"file2");
  5. open (my $fh3,">","file3");

  6. my %hash;
  7. my $t="";

  8. while (<$fh1>){
  9.         if($_!~/word0/ and $_!~/^$/){
  10.                 if($_!~/^\d/){$t.=$_;}
  11.                 else{$hash{$t}.=$_ ;}
  12.                 }
  13.         else{$t="";
  14.                 }
  15. }
  16. while (<$fh2>){
  17.         if($_!~/word0/ and $_!~/^$/){
  18.                 if($_!~/^\d/){$t.=$_;}
  19.                 else{$hash{$t}.=$_ ;}
  20.                 }
  21.         else{$t="";
  22.                 }
  23.         }
  24. my %hash2;
  25. foreach my $p (keys %hash) {
  26.         print $fh3 $p;
  27.         $hash2{substr($_,0,index($_,"\."))}=$_ foreach (split /\n/,$hash{$p});
  28.         print $fh3 $hash2{$_},"\n" foreach sort {$a<=>$b} (keys %hash2);
  29.         print $fh3 "word0\n\n";
  30.         undef(%hash2);
  31.         }

  32. close($fh1);
  33. close($fh2);
  34. close($fh3);
复制代码

作者: jhinux    时间: 2011-08-18 11:48
这个非得用perl?
作者: jiujiujiu338    时间: 2011-08-18 12:19
回复 27# jhinux


   嗯呐,因为想学习下perl
作者: jiujiujiu338    时间: 2011-08-18 13:12
回复  jiujiujiu338
yinyuemi 发表于 2011-08-18 11:36



    多谢多谢,这个成功了,可是有个地方没有看懂,还请帮忙讲一下$hash2{substr($_,0,index($_,"\."))}=$_ foreach (split /\n/,$hash{$p});麻烦了,多谢
作者: yinyuemi    时间: 2011-08-18 13:19
回复 29# jiujiujiu338

  1. split /\n/ $hash{$p} # 把$hash{$p}的值,split 成一个数组,分隔符是'\n'
  2. foreach 函数读取上面的数组值,默认下用$_
  3. substr函数截取,比如“13.081 11.386”字符串中的13
  4. $hash2{}=$_ #建一个hash,key是 13,value是 “13.081 11.386”

复制代码

作者: jiujiujiu338    时间: 2011-08-18 13:30
回复 30# yinyuemi


    哦,明白了,多谢了,我想解决这样一个问题,可是还是不会,还请帮忙看看

合并.rar

3.55 KB, 下载次数: 15


作者: yinyuemi    时间: 2011-08-18 14:13
本帖最后由 yinyuemi 于 2011-08-18 14:18 编辑

回复 31# jiujiujiu338
  1. #!/bin/perl
  2. use strict;

  3. open (my $fh1,"file1");
  4. open (my $fh2,"file2");
  5. open (my $fh3,">","file3");

  6. my %hash1;
  7. my %hash2;
  8. my %hash3;
  9. my $t="";
  10. my $tmp="";

  11. while (<$fh1>){
  12.         if($_!~/end/ and $_!~/^$/){
  13.                 if($_!~/^\d/){
  14.                         $tmp=$1 if(/(name=\S+)\s+.*/);
  15.                         {$t.=$_;}
  16.                 }
  17.                 else{$hash1{$t}.=$_ ;}
  18.                 }
  19.         else{$hash2{$tmp}=$t if($_=~/end/);
  20.                 $t="";
  21.                 $tmp="";
  22.                 }
  23. }
  24. while (<$fh2>){
  25.         if($_!~/end/ and $_!~/^$/){
  26.                 if($_!~/^\d/){
  27.                         $tmp=$1 if(/(name=\S+)\s+.*/);
  28.                         {$t.=$_;}
  29.                 }
  30.                 else{$hash1{$t}.=$_ ;}
  31.                 }
  32.         else{
  33.                 if($_=~/end/){
  34.                         if(exists($hash2{$tmp})){$hash1{$hash2{$tmp}}.=$hash1{$t};
  35.                                 delete $hash1{$t};}
  36.                         else{$hash2{$tmp}=$t;}
  37.                 }
  38.                 $t="";
  39.                 $tmp="";
  40.                 }
  41.         }

  42. foreach my $p (keys %hash1) {
  43.         print $fh3 $p;
  44.         $hash3{substr($_,0,index($_,"\."))}=$_ foreach (split /\n/,$hash1{$p});
  45.         print $fh3 $hash3{$_},"\n" foreach sort {$a<=>$b} (keys %hash3);
  46.         print $fh3 "end\n\n";
  47.         undef(%hash3);
  48.         }

  49. close($fh1);
  50. close($fh2);
  51. close($fh3);
复制代码

作者: jiujiujiu338    时间: 2011-08-18 14:47
回复  jiujiujiu338
yinyuemi 发表于 2011-08-18 14:13



    太感谢了,我要好好学习学习这个~~~
这里边有个小问题:数字中有的是整数部分是一样的,小数部分不同,只按照整数部分排序不行,如file1中的一、二行数据
   11.1 81.84
     11.9 11.386
结果11.1 81.84没有了。
另外需要按照name的world1,world2,world3的顺序来

已经非常的感谢了
作者: jiujiujiu338    时间: 2011-08-18 15:48
回复 32# yinyuemi


    嘿嘿,我个白痴,第一个小问题只要把substr($_,0,index($_,"\.")改为substr($_,0,index($_," ")就行了
现在还有第二个小问题,按照name中的word1,word2,word3排序
作者: jiujiujiu338    时间: 2011-08-18 16:37
回复 34# jiujiujiu338


    第二个小问题我会啦,在foreach my $p  (keys %hash1)  加个(sort {$a cmp $b}就可以了,多谢啊
作者: jason680    时间: 2011-08-18 16:46
回复  jiujiujiu338


    第二个小问题我会啦,在foreach my $p  (keys %hash1)  加个(sort {$a cmp $ ...
jiujiujiu338 发表于 2011-08-18 16:37



sort缺省就是sort {$a cmp $b}
加sort就可以(sort keys %hash1)
作者: jiujiujiu338    时间: 2011-08-18 21:05
回复 32# yinyuemi


   哎,我想按照word1、word2、word3的顺序排序,加上sort keys $hash1后,不对啊,如果有word10,那样word10就在word2前边了,怎么办啊,求解,多谢~
作者: jiujiujiu338    时间: 2011-08-18 21:08
回复 36# jason680


    是是是,多谢提醒
作者: jiujiujiu338    时间: 2011-08-19 09:41
回复 32# yinyuemi


    哎,我想按照word1、word2、word3的顺序排序,加上sort keys $hash1后,不对啊,如果有word10,那样word10就在word2前边了,怎么办啊,求解,多谢~
作者: yinyuemi    时间: 2011-08-19 13:36
回复 39# jiujiujiu338
  1. #!/bin/perl
  2. use strict;

  3. open (my $fh1,"file1");
  4. open (my $fh2,"file2");
  5. open (my $fh3,">","file3");

  6. my %hash1;
  7. my %hash2;
  8. my %hash3;
  9. my %hash4;
  10. my $t="";
  11. my $tmp="";

  12. while (<$fh1>){
  13.         if($_!~/end/ and $_!~/^$/){
  14.                 if($_!~/^\d/){
  15.                         $tmp=$1 if(/(name=\S+)\s+.*/);
  16.                         {$t.=$_;}
  17.                 }
  18.                 else{$hash1{$t}.=$_ ;}
  19.                 }
  20.         else{$hash2{$tmp}=$t if($_=~/end/);
  21.                 $t="";
  22.                 $tmp="";
  23.                 }
  24. }
  25. while (<$fh2>){
  26.         if($_!~/end/ and $_!~/^$/){
  27.                 if($_!~/^\d/){
  28.                         $tmp=$1 if(/(name=\S+)\s+.*/);
  29.                         {$t.=$_;}
  30.                 }
  31.                 else{$hash1{$t}.=$_ ;}
  32.                 }
  33.         else{
  34.                 if($_=~/end/){
  35.                         if(exists($hash2{$tmp})){$hash1{$hash2{$tmp}}.=$hash1{$t};delete $hash1{$t};}
  36.                         else{$hash2{$tmp}=$t;}
  37.                 }
  38.                 $t="";
  39.                 $tmp="";
  40.                 }
  41.         }

  42. foreach my $p (keys %hash1) {
  43.         my $key1=$1 if($p=~/.*\nname=word(\d+).*/s);
  44.         $hash4{$key1}.=$p;
  45.         $hash3{substr($_,0,index($_," "))}=$_ foreach (split /\n/,$hash1{$p});
  46.         $hash4{$key1}.=$hash3{$_}."\n" foreach sort {$a<=>$b} (keys %hash3);
  47.         $hash4{$key1}.="end\n\n";
  48.         undef(%hash3);
  49.         }

  50. foreach (sort {$a<=>$b} (keys %hash4)) { print $fh3 $hash4{$_};}
  51.        
  52.        
  53. close($fh1);
  54. close($fh2);
  55. close($fh3);
复制代码

作者: jiujiujiu338    时间: 2011-08-20 12:29
回复 40# yinyuemi


    多谢啊
作者: jiujiujiu338    时间: 2011-09-03 21:29
回复 3# zhlong8


    帮帮忙,好兄弟
求教:下面的程序运行时总是显示Use of uninitialized value $destination in hash element,uninitialized value $bytes,这是咋回事啊?
运行结果:
coconet.dat: 0
coconet.dat=>transferred bytes is 0 bytes
其实coconet.dat 是个文件,我open fh,“coconet.dat”;总说我打开文件句柄只是为了输出,求高人指教
#!/urs/bin/perl -w
use strict;
my %total_bytes;
my $all = "all machines";

while (<coconet.dat>) {
        next if (/^#/);
        my ( $source, $destination, $bytes ) = split;
        $total_bytes{$source}{$destination} += $bytes;
        $total_bytes{$source}{$all}         += $bytes;
}
my @sources =
  sort { $total_bytes{$b}{$all} <=> $total_bytes{$a}{$all} } keys %total_bytes;                                            
for my $source (@sources) {

        my @destinations =
          sort { $total_bytes{$source}{$b} <=> $total_bytes{$source}{$a} }
          keys %{ $total_bytes{$source} };
        print  "$source: $total_bytes{$source}{$all}";
        for my $destination (@destinations) {
                next if $destination eq $all;
                print  "$source=>$destination",
                "transferred bytes is $total_bytes{$sources}{$destination} bytes\n";
                  
        }
        print "\n";
}
作者: zhlong8    时间: 2011-09-03 23:30
回复  zhlong8


    帮帮忙,好兄弟
求教:下面的程序运行时总是显示Use of uninitialized value $de ...
jiujiujiu338 发表于 2011-09-03 21:29



    读文件的方式错了

open my $fh, '<', 'coconet.dat' or die $!;

while (<$fh>) {
    ...
}
作者: jiujiujiu338    时间: 2011-09-05 04:09
回复 43# zhlong8


    太爱你了
作者: jiujiujiu338    时间: 2011-09-06 05:28
回复 43# zhlong8
求教高手,问什么下面生成的data.pl 里面总是有乱码,,,,,,疯了。
    #!/urs/bin/perl -w
use strict;
use Storable;
my %total_bytes;
my $all       = "all machines";
my $data_file = "data.pl";
if ( -e $data_file ) {
        my $data = retrieve '$data_file';
        %total_bytes = %$data;
}
open my $fh, '<', 'coconet.dat' or die $!;
while (<$fh>) {
        next if (/^#/);
        my ( $source, $destination, $bytes ) = split / /;
        $total_bytes{$source}{$destination} += $bytes;
        $total_bytes{$source}{$all}         += $bytes;
}
store [ \%total_bytes ], $data_file;
my @sources =
  sort { $total_bytes{$b}{$all} <=> $total_bytes{$a}{$all} }
  keys %total_bytes;                          # sort houmian {}

for my $source (@sources) {

        my @destinations =
          sort { $total_bytes{$source}{$b} <=> $total_bytes{$source}{$a} }
          keys %{ $total_bytes{$source} };
        print "$source: $total_bytes{$source}{$all}\n";
        for my $destination (@destinations) {
                next if $destination eq $all;
                print "$source=>$destination",
                  "  transferred bytes is $total_bytes{$source}{$destination} bytes\n";

        }
        print "\n";
}

close $fh;




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