繁体   English   中英

列中的模式匹配

[英]Pattern Matching in Columns

文件1

A11;F1;BMW
A23;F2;BMW
B12;F3;BMW
H11;F4;JBW

文件2

P01;A1;0;0--00  ;123;456;150
P01;A11;0;0--00  ;123;444;208
P01;B12;0;0--00  ;123;111;36
P01;V11;0;0--00  ;123;787;33.9

输出量

-;-;-;P01;A1;0;0--00  ;123;456;150
A11;F1;BMW;P01;A11;0;0--00  ;123;444;208
B12;F3;BMW;P01;B12;0;0--00  ;123;111;36
-;-;-;P01;V11;0;0--00  ;123;787;33.9

我试过了

awk 'FNR==NR {a[$2] = $0; next }{ if($1 in a) {p=$1;$1="";print a[p],$0}}' File1 File2 

但没有工作。

基本上,我想从FILE 1获取详细信息并与FILE2(主列表)进行比较。

范例:

FILE2中的A1在FILE1中不可用,因此在输出文件中,第三个字段的第一个字段为“-”,其余部分为FILE2。 现在,我们有了A11,并在FILE1中获得了详细信息。 因此,我们从文件1和文件2中写入了A11的详细信息

我个人会在Perl中执行此操作,但是由于每个人及其母亲都在为您提供Perl解决方案,因此可以采用以下替代方法:

假设每个文件中的记录具有一致的字段数,并且每个文件中的记录均按字典顺序按“ join”字段排序,则可以使用join

join -1 1 -2 2 -t ';' -e - -o '1.1 1.2 1.3 2.1 2.2 2.3 2.4 2.5 2.6 2.7' -a 2 File1 File2

选项说明:

  • -1 1-2 2意味着“加入”字段( A11A23 ,等)是在第一场File1和在第二个字段File2
  • -t ';' 表示字段被分隔;
  • -e -表示应将空字段替换为-
  • -o '1.1 1.2 1.3 2.1 2.2 2.3 2.4 2.5 2.6 2.7'意味着想要每个输出线到由来自前三个字段的File1 ,随后从所述第一七个字段File2 (这就是为什么这种方法要求每个文件中的记录具有一致数量的字段的原因。)
  • -a 2意味着要包括每行File2的输出,即使有一个从没有相应的行File1 (否则,它将仅输出两个文件中都具有匹配项的行。)

Perl常用的方法:使用哈希记住主列表:

#!/usr/bin/perl
use warnings;
use strict;

my %hash;

open my $MASTER, '<', 'File1' or die $!;
while (<$MASTER>) {
    chomp;
    my @columns = split /;/;
    $hash{$columns[0]} = [@columns[1 .. $#columns]];
}
close $MASTER;

open my $DETAIL, '<', 'File2' or die $!;
while (<$DETAIL>) {
    my @columns = split /;/;
    if (exists $hash{$columns[1]}) {
        print join ';', $columns[1], @{ $hash{$columns[1]} }, q();
    } else {
        print '-;-;-;';
    }
    print;
}
close $DETAIL;

使用Perl:

use warnings;
use strict;
my %file1;
open (my $f1, "<", "file1") or die();
while (<$f1>) {
  chomp;
  my @v = (split(/;/))[0];
  $file1{$v[0]} = $_; 
}
close ($f1);
open (my $f2, "<", "file2") or die();
while (<$f2>) {
  chomp;
  my $v = (split(/;/))[1];
  if (defined $file1{$v}) {
    print "$file1{$v};$_\n";
  } else {
    print "-;-;-;$_\n";
  }
}
close ($f2);

在单行程序中,这很不方便,因为它涉及读取两个输入文件,但是问题并不难

该程序从file1读取所有行,并使用第一个字段作为键将行存储在哈希中

然后,读取来自file2所有行,并将第二个字段用作访问哈希的键。 //定义或运算符用于打印元素的值(如果存在)或默认字符串(如果不存在)

最后,打印出file2的当前行

use strict;
use warnings;

my %data;

open my $fh, '<', 'file1' or die $!;
while (<$fh>) {
  chomp;
  my $key = (split /;/)[0];
  $data{$key} = $_;
}

open $fh, '<', 'file2' or die $!;
while (<$fh>) {
  my $key = (split /;/)[1];
  print $data{$key} // '-;-;-;', $_;
}

输出

-;-;-;P01;A1;0;0--00  ;123;456;150
A11;F1;BMWP01;A11;0;0--00  ;123;444;208
B12;F3;BMWP01;B12;0;0--00  ;123;111;36
-;-;-;P01;V11;0;0--00  ;123;787;33.9

perl解决方案可能包括非常好的模块Text :: CSV 如果是这样,您可以将值提取到哈希中,然后再使用该哈希进行查找。 查找值时,您将插入空白值-;-;-; 查找哈希中任何未定义的值。

use strict;
use warnings;
use Text::CSV;

my $lookup = "file1.csv";   # whatever file is used to look up fields 0-2
my $master = "file2.csv";   # the file controlling the printing

my $csv = Text::CSV->new({
        sep_char    => ";", 
        eol         => $/,  # to add newline to $csv->print()
        quote_space => 0,   # to avoid adding quotes 
    });

my %lookup;

open my $fh, "<", $lookup or die $!;

while (my $row = $csv->getline($fh)) {
    $lookup{$row->[0]} = $row;    # add entire row to specific key
}
open $fh, "<", $master or die $!; # new $fh needs no close

while (my $row = $csv->getline($fh)) {
    my $extra = $lookup{$row->[1]} // [ qw(- - -) ]; # blank row if undef
    unshift @$row, @$extra;       # add the new values
    $csv->print(*STDOUT, $row);   # then print them
}

输出:

-;-;-;P01;A1;0;0--00  ;123;456;150
A11;F1;BMW;P01;A11;0;0--00  ;123;444;208
B12;F3;BMW;P01;B12;0;0--00  ;123;111;36
-;-;-;P01;V11;0;0--00  ;123;787;33.9

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM