-4

To extend a column (col_F) into the final output (File3) by comparing File 1 and File2.

Col_A has possessed a relationship between File1 and File2. I have to use this relationship and omit values in the col_F into File3.

For an example:

File1
col_A       col_F                          
 A1      F1
 A2      F2
 A5      F5
 AZ      FZ
 AX      FX
 A#      F#
 A2      F2


File2  
col_A  col_B      col_C   col_D
  A1    B        C       D
  A2    B        C       D
  A5    B        C       D
  AZ    B        C       D
  AX    B        C       D
  A#    B        C       D
  A2    B        C       D


File3_Final output (Query result) 
col_A  col_B    col_C   col_D   col_F
A1      B        C        D      F1
A2      B        C        D      F2
A5      B        C        D      F5
A       B        C        D      FZ
AX      B        C        D      FX
A#      B        C        D      F#
A2      B        C        D      F2


NOTE: All the Tables are saved in .csv format
4

1 回答 1

1

你说你想要 Python、MySQL 或 MongoDB 中的解决方案。但是你已经用“perl”标记了这个问题。所以这里有一个 Perl 解决方案。

#!/usr/bin/perl

use strict;
use warnings;

my %file1 = get_file1();

open my $fh2, '<', 'File2' or die "File 2: $!\n";

chomp(my $header = <$fh2>);
print "$header\tcol_F\n";

while (<$fh2>) {
  chomp;
  my $colA = (split ' ')[0];
  print "$_\t$file1{$colA}\n";
}

sub get_file1 {
  my %hash;

  open my $fh1, '<', 'File1' or die "File 1: $!\n";

  <$fh1>; # skip headers
  while (<$fh1>) {
    chomp;
    my ($key, $val) = split ' ';
    $hash{$key} = $val;
  }

  return %hash;
}
于 2019-12-03T09:01:10.267 回答