Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- #
- # https://www.linux.org.ru/forum/development/12024408
- #
- use strict;
- use warnings;
- my ( $infile, @files ) = @ARGV;
- if ( defined $infile && @files > 0 ) {
- &compare( $infile, @files );
- } else {
- &print_usage();
- exit 1;
- }
- exit 0;
- sub compare($@) {
- my ( $infile, @files ) = @_;
- # read main file and build uniq lines hash
- open my $fh, "<", $infile or die "open $infile: $!";
- my %uniq;
- while ( my $line = <$fh> ) {
- my @counters;
- push @counters, 0 for @files;
- chomp $line;
- $uniq{ $line } = \@counters;
- }
- close $fh or die "close $infile: $!";
- # read each file to fill counters
- for ( my $filenum = 0; $filenum < @files; $filenum++ ) {
- my $file = $files[ $filenum ];
- open my $fh, "<", $file or die "open $file: $!";
- while ( my $line = <$fh> ) {
- chomp $line;
- next if not exists $uniq{ $line };
- $uniq{ $line }->[ $filenum ]++;
- }
- close $fh or die "close $file: $!";
- }
- # find missing lines
- LINES: for my $line ( keys %uniq ) {
- my $counters = $uniq{ $line };
- $_ > 0 and next LINES for ( @$counters );
- # output uniq
- print $line, "\n";
- }
- }
- sub print_usage {
- print <<'EOF';
- Usage:
- xcmp.pl input_file file1 [ file2 file 3 ... fileN ]
- EOF
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement