#!/usr/local/bin/perl use 5.016; #version >=5.12 turns on strictures by default use warnings; use autodie qw/ open close /; #saves typing die all the time die "Usage: $0 infile1 infile2\n" unless @ARGV == 2; my %count_cols; my @cols; while( <> ) { # perl will read/process one file in whole then move on to the next chomp; # get 1st col from each line using an array slice push @cols, ( split( /\t/, $_ ) )[0]; } for ( @cols ) { $count_cols{ $_ }++; # hash value = how many times does col name appear } my @intersects; # store columns that appear in both files for ( keys %count_cols ) { if( $count_cols{ $_ } == 2 ) { push @intersects, $_; } } # open fileA again and extract fields in second column open IN, "<", "fileA"; while( my $line = ) { chomp $line; next if ! $line; for my $intersect ( @intersects ) { if( $intersect eq ( split( /\t/, $line ) )[0] ) { my $result = ( split( /\t/, $line ) )[1]; say $result; # prints with \n, supported in >5.10 } } } close IN;