Perl's equivalent of "grep -f"

Thursday, January 10, 2013 , , 0 Comments

Lets say there are two files:

> cat file1
693661
11002
10995
48981
79600
> cat file2
10993   item    0
11002   item    6
10995   item    7
79600   item    7
439481  item    5
272557  item    7
224325  item    7
84156   item    6
572546  item    7
693661  item    7

using
grep -f file1 file2
we can filter the data in file2 but comparing the matching rows only with teh first column.

I have written a perl equivalent to this but yet not in a complete way.
the below command will simply compare the first field of the file 1 and present the output if it matches with the first field of file2.

> perl -F -lane 'BEGIN{$c=1}if($c==1){$x{$_}++};if(eof && $c==1){$c++;next};if($c==2){if($x{$F[0]}){print }}' file1 file2
11002   item    6
10995   item    7
79600   item    7
693661  item    7

0 comments: