Chinaunix

标题: 如何把2.txt里面有的行,但是1.txt里面没有的行,写到3.txt里面? [打印本页]

作者: xiaomm250    时间: 2014-04-11 16:27
标题: 如何把2.txt里面有的行,但是1.txt里面没有的行,写到3.txt里面?
如何把2.txt里面有的行,但是1.txt里面没有的行,写到3.txt里面?
1.txt与2.txt文件里面的内容很接近,
那么请问如何把2.txt里面有的行,但是1.txt里面没有的行,
写到3.txt里面呢?
作者: xiaomm250    时间: 2014-04-11 16:28
本人perl菜鸟一枚, 只会些简单的正则表达式这种弱智的,复杂的不会
学过简单的c语言
作者: q1208c    时间: 2014-04-11 16:48
本帖最后由 q1208c 于 2014-04-11 16:48 编辑
  1. diff 1.txt 2.txt | sed -n '/^> /s/^> //p' > 3.txt
复制代码

作者: xiaomm250    时间: 2014-04-11 16:51
q1208c 发表于 2014-04-11 16:48


我想的是perl
作者: xiaomm250    时间: 2014-04-11 17:26
本帖最后由 xiaomm250 于 2014-04-11 17:28 编辑

把1.txt文件里面的内容赋值给数组a
把2.txt文件里面的内容赋值给数组b
foreach i  in a
    k=0
    foreach j in b
        if i等于j then
             k=k+1
             退出本层循环
        end
    end
    if k>0 then
        输出i到3.txt
    end
end
这个是我大致的思路,如何用perl语言来描述呢?????????
作者: q1208c    时间: 2014-04-11 17:50
回复 5# xiaomm250

这要看你的文件有多大. 如果只有几行, 当然没问题. 如果有上百万行. 你得多少内存呀.
   
作者: xiaomm250    时间: 2014-04-11 18:09
本帖最后由 xiaomm250 于 2014-04-11 18:10 编辑
q1208c 发表于 2014-04-11 17:50
回复 5# xiaomm250

这要看你的文件有多大. 如果只有几行, 当然没问题. 如果有上百万行. 你得多少内存呀 ...
  1. #2.txt有的,但是1.txt却没有的,把这些行输出到3.txt
  2. open(File1,'<1.txt') or die "can not open file:$!\n";
  3. open(File2,'<2.txt') or die "can not open file:$!\n";
  4. open(File3,'>3.txt') or die "can not open file:$!\n";
  5. foreach $line2(<File2>)
  6. {
  7.     $k=0;
  8.     foreach $line1(<File1>)
  9.     {
  10.         if ($line2 eq $line1)
  11.         {
  12.             $k=$k+1;
  13.         }
  14.     }
  15.     if ($k==0)
  16.     {
  17.         print File3 $line2;
  18.     }
  19. }
  20. close(File1);
  21. close(File2);
  22. close(File3);
复制代码
为什么我的这个代码会没用呢??????????
作者: q1208c    时间: 2014-04-11 19:04
回复 7# xiaomm250

    http://search.cpan.org/~adamk/Text-Diff-1.37/lib/Text/Diff.pm

这个可能是你需要的.

作者: xiaomm250    时间: 2014-04-11 19:48
本帖最后由 xiaomm250 于 2014-04-11 20:00 编辑

谁能给一个代码?
作者: substr函数    时间: 2014-04-11 20:15
大致的思路
  1. open( File1, '<1.txt' ) or die "can not open file:$!\n";
  2. open( File2, '<2.txt' ) or die "can not open file:$!\n";
  3. open( File3, '>3.txt' ) or die "can not open file:$!\n";

  4. my %F1 = map { $_, 1 } <File1>;

  5. while (<File2>) {
  6.     print File3 $_ unless exists $F1{$_};
  7. }
复制代码

作者: xiaomm250    时间: 2014-04-13 15:30
substr函数 发表于 2014-04-11 20:15
大致的思路

感觉运行结果是正确的

把这句
my %F1 = map { $_, 1 } <File1>;
换成这句
my %F1 = map { $_ } <File1>;
为什么结果不对呢?
作者: stanley_tam    时间: 2014-04-13 17:54
假设文件不大,2.txt文件木有重复
下面也许能行{:3_188:}
  1. #!perl
  2. use Modern::Perl;
  3. use Set::Scalar;
  4. use File::Slurp qw{ read_file write_file };

  5. my @files  = ('1.txt', '2.txt', '3.txt');

  6. # save file lines to set
  7. my $set_for_file2 = Set::Scalar->new( read_file($files[2]) );
  8. my $set_for_file1 = Set::Scalar->new( read_file($files[1]) );

  9. my $set_for_file3 = $set_for_file2 - $set_for_file1;

  10. write_file($files[3], sort $set_for_file3->elements);

  11. __END__
复制代码





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