Advertisement
Guest User

Untitled

a guest
Oct 14th, 2015
113
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2.  
  3. #
  4. # https://www.linux.org.ru/forum/development/12024408
  5. #
  6.  
  7. use strict;
  8. use warnings;
  9.  
  10.  
  11. my ( $infile, @files ) = @ARGV;
  12.  
  13. if ( defined $infile && @files > 0 ) {
  14.     &compare( $infile, @files );
  15. } else {
  16.     &print_usage();
  17.     exit 1;
  18. }
  19.  
  20. exit 0;
  21.  
  22.  
  23. sub compare($@) {
  24.     my ( $infile, @files ) = @_;
  25.  
  26.  
  27.     # read main file and build uniq lines hash
  28.     open my $fh, "<", $infile or die "open $infile: $!";
  29.  
  30.     my %uniq;
  31.     while ( my $line = <$fh> ) {
  32.         my @counters;
  33.         push @counters, 0 for @files;
  34.         chomp $line;
  35.         $uniq{ $line } = \@counters;
  36.     }
  37.  
  38.     close $fh or die "close $infile: $!";
  39.  
  40.  
  41.     # read each file to fill counters
  42.     for ( my $filenum = 0; $filenum < @files; $filenum++ ) {
  43.         my $file = $files[ $filenum ];
  44.         open my $fh, "<", $file or die "open $file: $!";
  45.  
  46.         while ( my $line = <$fh> ) {
  47.             chomp $line;
  48.             next if not exists $uniq{ $line };
  49.             $uniq{ $line }->[ $filenum ]++;
  50.         }
  51.  
  52.         close $fh or die "close $file: $!";
  53.     }
  54.  
  55.     # find missing lines
  56.     LINES: for my $line ( keys %uniq ) {
  57.         my $counters = $uniq{ $line };
  58.         $_ > 0 and next LINES for ( @$counters );
  59.         # output uniq
  60.         print $line, "\n";
  61.     }
  62. }
  63.  
  64. sub print_usage {
  65.     print <<'EOF';
  66. Usage:
  67.     xcmp.pl input_file file1 [ file2 file 3 ... fileN ]
  68. EOF
  69. }
Advertisement
RAW Paste Data Copied
Advertisement