#! /usr/bin/perl use strict; use warnings; # file paths; input for reading, output for writing my $input1 = 'input1.txt'; my $input2 = 'input2.txt'; my $output = 'output.txt'; # open files; input for reading, output for writing open my $in1fh, '<', $input1 or die "open($input1,ro) failed: $!\n"; open my $in2fh, '<', $input2 or die "open($input2,ro) failed: $!\n"; open my $outfh, '>', $output or die "open($output,w) failed: $!\n"; # read linewise from file input1 while ( my $line1 = <$in1fh> ) { # read line from file input2 my $line2 = <$in2fh>; # extract email adresses from both lines # email is at the beginning of each line and separated with ';' from the rest of the line # user@example.org;additional data ... my $email1 = ( split m{;}, $line1, 2 )[0]; my $email2 = ( split m{;}, $line2, 2 )[0]; # if emails differ, write to output file if ( $email1 ne $email2 ) { print $outfh, $line1, $line2; } } # end of file of input1 reached # if there are lines left in input2, read them linewise and write them to output while ( my $line2 = <$in2fh> ) { print $outfh $line2; } # close all files close $outfh or die "close($output) failed: $!\n"; close $in1fh or die "close($input1) failed: $!\n"; close $in2fh or die "close($input2) failed: $!\n";