@ARGV or die "No input file specified"; open my $first, '<', $ARGV[0] or die "Unable to open input file: $!"; open my $second, '<', $ARGV[1] or die "Unable to open input file: $!"; print scalar(<$first>); my $secondHeader = <$second>; while (<$first>) { @cols = split /\s+/; $p1 = $cols[1]; $p2 = $cols[2]; my $secondLine = <$second>; if ( defined $secondLine ) { @sec = split( /\s+/, $secondLine ); print join( "\t", @cols ), "\n" if ( $p1>=$sec[0] && $p2<=$sec[1] || $p1<=$sec[0] && $p2>=$sec[1] ); } }