Skip to content

Instantly share code, notes, and snippets.

@pkrnjevic
Created April 13, 2012 05:02
Show Gist options
  • Save pkrnjevic/2373832 to your computer and use it in GitHub Desktop.
Save pkrnjevic/2373832 to your computer and use it in GitHub Desktop.
Simple script to find strings common to all files specified on command line. (the intersection)
#!/usr/bin/env perl
# Read each file specified on command line and print lines found in all files
#
open(FH,$ARGV[0]); chomp(@isect = <FH>); close(FH);
shift(@ARGV);
while (@ARGV) {
## read each file into an array and strip newlines
open(FH,$ARGV[0]); chomp(@f = <FH>); close(FH);
shift(@ARGV);
@isect = intersection(@isect,@f);
}
print "$_\n" foreach (@isect);
sub intersection {
(@a, @b) = @_;
@union = @isect = ();
%union = %isect = ();
foreach $e (@a, @b) { $union{$e}++ && $isect{$e}++};
@isect = keys %isect;
return (@isect);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment