| 1 |
#! /usr/bin/perl |
| 2 |
use warnings; |
| 3 |
use strict; |
| 4 |
use integer; |
| 5 |
|
| 6 |
our $usage = <<END; |
| 7 |
usage: $0 big small |
| 8 |
(replace either filename with '-' for stdin) |
| 9 |
END |
| 10 |
|
| 11 |
# This script subtracts the contents of a "small" file from those of |
| 12 |
# a "big." It outputs the difference. (Note: this script does not |
| 13 |
# attempt to detect when the small file contains something the big file |
| 14 |
# does not, nor does it require the small file actually to be smaller |
| 15 |
# than the big. It merely reports lines in the big file which the small |
| 16 |
# lacks.) |
| 17 |
# |
| 18 |
# This script does not respond to options---not even to -h. However, if |
| 19 |
# you invoke it without an argument it will exit with an error and print |
| 20 |
# to stderr a usage message. |
| 21 |
|
| 22 |
# Preliminaries. |
| 23 |
@ARGV == 1 || @ARGV == 2 or die $usage; |
| 24 |
my $big = shift; |
| 25 |
my $small = shift; |
| 26 |
defined $small or $small = '-'; |
| 27 |
$big ne '-' or $small ne '-' |
| 28 |
or die "$0: stdin cannot be both big and small\n"; |
| 29 |
my( $fh_big, $fh_small ); |
| 30 |
for ( |
| 31 |
{ filename => \$big , fh => \$fh_big }, |
| 32 |
{ filename => \$small, fh => \$fh_small }, |
| 33 |
) { |
| 34 |
if ( ${ $_->{filename} } eq '-' ) { ${ $_->{fh} } = \*STDIN } |
| 35 |
else { open ${ $_->{fh} }, '<', ${ $_->{filename} } } |
| 36 |
} |
| 37 |
|
| 38 |
# Here is the main program. In Perl, it is very short. |
| 39 |
my %small = map {$_=>1} <$fh_small>; |
| 40 |
print for grep { !$small{$_} } <$fh_big>; |
| 41 |
|