English 中文(简体)
您如何从另一个档案中查阅第一件档案中的两条线?
原标题:How do you retrieve a set of two lines from one file referencing the first from the other file?

files

$cat file 1
Index1 annotation1
abcd
Index2 annotation2
efgh
Index3 annotation3
hijk
Index4 annotation4
lmno
Index5 annotation5
pqrs
…
$cat file2
Index1
Index3
Index5

我想得到的是第1号档案中的线路清单,以及随后检索的每条线。

Index1 annotation1
abcd
Index3 annotation3
hijk
Index5 annotation5
pqrs

My current solution is to use grep and its file flag grep -A 1 --file="file2" file1 | awk !/--/

但是,我很想知道,是否有更明智的解决办法。 目前的解决办法需要很长的时间,因为档案是巨大的。

最佳回答
#!/usr/bin/env perl

use strict; use warnings;
use autodie;

my %to_index;

my ($annotations_file, $index_file) = @ARGV;

open my $index,  < , $index_file;

while (my $line = <$index>) {
    next unless $line =~ /S/;
    chomp $line;
    $to_index{ $line } = undef;
}

close $index;

open my $annotations,  < , $annotations_file;

while (my $line = <$annotations>) {
    next unless $line =~ /S/;
    my ($keyword) = ($line =~ /^(S+)/);
    if (exists $to_index{ $keyword }) {
        print $line;
        print scalar <$annotations>;
    }
}

close $annotations;
问题回答

我建议通过档案1进行阅读,以编制每个标识在档案中的位置索引。 所需数据的标签可从文件2和查阅的索引中读到,以便了解哪些数据为相应的信息。

该方案体现了这一原则。 如何区分标签和检验的其余部分是明确的。 我假定,它们都是从<条码>Index开始的,这可能是错误的,但如果你需要帮助使其适应你的真实数据,请再次问。

use strict;
use warnings;

@ARGV = qw/ file1.txt file2.txt / unless @ARGV;
my ($file1, $file2) = @ARGV;

my %index;

open my $f1,  < , $file1 or die qq(Unable to open "$file1": $!);
my $pos = tell $f1;
while (<$f1>) {
  $index{$1} = $pos if /^(IndexS+)/;
  $pos = tell $f1;
}

open my $f2,  < , $file2 or die qq(Unable to open "$file2": $!);
while (<$f2>) {
  next unless /^(IndexS+)/ and defined($pos = $index{$1});
  seek $f1, $pos, 0;
  print scalar <$f1>, scalar <$f1>;
}

Index1 annotation1
abcd
Index3 annotation3
hijk
Index5 annotation5
pqrs




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签