#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
#@ARGV = ('file1.txt','file2.txt','file3.txt',2,3);
my ($file1, $file2, $file3, $file1col,$file2col) = @ARGV;
my %hash = ();
open my $fh1,'<', $file1
or die "Could not open for read $file1 : $!";
while ( my $row = <$fh1> ) {
chomp $row;
next if $row =~ /^\s*$/;
# extract the selected column only
my @fields = split /\|/, $row ;
my $key = $fields[$file1col-1];
$key =~ s/^\s+|\s+$//g;# trim spaces
$hash{$key} = 1;
}
close $fh1;
print Dumper \%hash;
open my $fh2,'<', $file2
or die "Could not open for read $file2 : $!";
open my $fh3,'>', $file3
or die "Could not open for write $file3 : $!";
while ( my $row = <$fh2> ) {
chomp $row;
next if $row =~ /^\s*$/g;
# extract the selected column only
my @fields = split /\|/, $row ;
my $key = $fields[$file2col-1];
$key =~ s/^\s+|\s+$//;# trim spaces
if ( exists $hash{$key} ) {
print "$row\n";
}
}
close $fh2;
close $fh3;
poj |