Skip to content

Instantly share code, notes, and snippets.

@taoliu
Created April 6, 2012 05:20
Show Gist options
  • Save taoliu/2317207 to your computer and use it in GitHub Desktop.
Save taoliu/2317207 to your computer and use it in GitHub Desktop.
find unique or common lines between two files
#!/usr/bin/perl -w
# Time-stamp: <2004-04-02 14:37:41 Tao Liu>
use strict;
if (@ARGV>0 and $ARGV[0] eq "-h") {
print "ediff : compute two sets with \'or diff and\'. \nUsage:\tediff [-h] : help, this info\n\tediff A or B : union of two sets\n\tediff A diff B : elements in
A but not in B\n\tediff A and B : intersection of two sets\n";
exit;
}
die "ediff needs _EXACTLY_ three paras!\nediff [-h] to get help...\n" if (@ARGV!=3);
open (IN1,"$ARGV[0]") || die "cannot open $ARGV[0]:$!\n";
open (IN2,"$ARGV[2]") || die "cannot open $ARGV[2]:$!\n";
if ("or" eq $ARGV[1]) {
#my %in1;
#my %in2;
my %out;
while(<IN1>) {
chomp;
$out{$_}=1;
}
while(<IN2>) {
chomp;
$out{$_}=1;
}
foreach (keys %out) {
print "$_\n";
}
} elsif ("diff" eq $ARGV[1]) {
#my %in1;
my %in2;
my %out;
while(<IN2>) {
chomp;
$in2{$_}=1;
}
while(<IN1>) {
chomp;
$out{$_}=1 if(not exists $in2{$_});
}
foreach (keys %out) {
print "$_\n";
}
} elsif ("and" eq $ARGV[1]) {
my %in1;
#my %in2;
my %out;
while(<IN1>) {
chomp;
$in1{$_}=1;
}
while(<IN2>) {
chomp;
$out{$_}=1 if(exists $in1{$_});
}
foreach (keys %out) {
print "$_\n";
}
} else {
print "\"$ARGV[1]\" is not a recognized command!\nediff [-h] to get help...\n" ;
exit;
}
close IN1;
close IN2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment