/usr/bin/combine is in moreutils 0.52.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | #!/usr/bin/perl
=head1 NAME
combine - combine sets of lines from two files using boolean operations
=head1 SYNOPSIS
combine file1 and file2
combine file1 not file2
combine file1 or file2
combine file1 xor file2
_ file1 and file2 _
_ file1 not file2 _
_ file1 or file2 _
_ file1 xor file2 _
=head1 DESCRIPTION
B<combine> combines the lines in two files. Depending on the boolean
operation specified, the contents will be combined in different ways:
=over 4
=item and
Outputs lines that are in file1 if they are also present in file2.
=item not
Outputs lines that are in file1 but not in file2.
=item or
Outputs lines that are in file1 or file2.
=item xor
Outputs lines that are in either file1 or file2, but not in both files.
=back
"-" can be specified for either file to read stdin for that file.
The input files need not be sorted, and the lines are output in the order
they occur in file1 (followed by the order they occur in file2 for the two
"or" operations). Bear in mind that this means that the operations are not
commutative; "a and b" will not necessarily be the same as "b and a". To
obtain commutative behavior sort and uniq the result.
Note that this program can be installed as "_" to allow for the syntactic
sugar shown in the latter half of the synopsis (similar to the test/[
command). It is not currently installed as "_" by default, but you can
alias it to that if you like.
=head1 SEE ALSO
join(1)
=head1 AUTHOR
Copyright 2006 by Joey Hess <joey@kitenet.net>
Licensed under the GNU GPL.
=cut
use warnings;
use strict;
sub filemap {
my $file=shift;
my $sub=shift;
open (IN, $file) || die "$file: $!\n";
while (<IN>) {
chomp;
$sub->();
}
close IN;
}
sub hashify {
my $file=shift;
my %seen;
filemap $file, sub { $seen{$_}++ };
return \%seen;
}
sub compare_or {
my ($file1, $file2) = @_;
filemap $file1, sub { print "$_\n" };
filemap $file2, sub { print "$_\n" };
}
sub compare_xor {
my ($file1, $file2) = @_;
my (@lines2, %seen2);
filemap $file2,
sub {
push @lines2, $_;
$seen2{$_} = 1;
};
# Print all lines in file1 that are not in file2,
# and mark lines that are in both files by setting
# their value in %seen2 to 0.
filemap $file1,
sub {
if (exists $seen2{$_}) {
$seen2{$_} = 0;
}
else {
print "$_\n";
}
};
# Print all lines that are in file2 but not in file1.
# The value of these lines in seen2 is set to 1.
foreach (@lines2) {
print "$_\n" if $seen2{$_};
}
}
sub compare_not {
my ($file1, $file2) = @_;
my $seen=hashify($file2);
filemap $file1, sub { print "$_\n" unless $seen->{$_} };
}
sub compare_and {
my ($file1, $file2) = @_;
my $seen=hashify($file2);
filemap $file1, sub { print "$_\n" if $seen->{$_} };
}
if (@ARGV >= 4 && $ARGV[3] eq "_") {
delete $ARGV[3];
}
if (@ARGV != 3) {
die "Usage: combine file1 OP file2\n";
}
my $file1=shift;
my $op=lc shift;
my $file2=shift;
if ($::{"compare_$op"}) {
no strict 'refs';
"compare_$op"->($file1, $file2);
}
else {
die "unknown operation, $op\n";
}
|