Find content of one file from another file in UNIX

前端 未结 8 600
攒了一身酷
攒了一身酷 2020-12-01 07:39

I have 2 files. First file contains the list of row ID\'s of tuples of a table in the database. And second file contains SQL queries with these row ID\'s in \"where\" clause

8条回答
  •  爱一瞬间的悲伤
    2020-12-01 08:23

    I suggest using a programming language such as Perl, Ruby or Python.

    In Ruby, a solution reading both files (f1 and f2) just once could be:

    idxes = File.readlines('f1').map(&:chomp)
    
    File.foreach('f2') do | line |
      next unless line =~ /where ri=(\d+);$/
      puts line if idxes.include? $1
    end
    

    or with Perl

    open $file, '<', 'f1';
    while (<$file>) { chomp; $idxs{$_} = 1; }
    close($file);
    
    open $file, '<', 'f2';
    while (<$file>) {
        next unless $_ =~ /where ri=(\d+);$/;
        print $_ if $idxs{$1};
    }
    close $file;
    

提交回复
热议问题