Created
April 13, 2012 05:02
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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