sdatta has asked for the wisdom of the Perl Monks concerning the following question:

I found the the following code for matching file by user Johngg
use strict; use warnings; open my $file1FH, q{<}, \ <<END_OF_FILE1 or die qq{open: $!\n}; 123456 123457 123458 END_OF_FILE1 chomp( my @file1Lines = <$file1FH> ); close $file1FH or die qq{close: $!\n}; my $rxFile1 = do { local $" = q{|}; qr{^(?:@file1Lines)}; }; open my $file2FH, q{<}, \ <<END_OF_FILE2 or die qq{open: $!\n}; 123456 foo 123456 bar 123457 foobar 123455 this would not be printed to new file END_OF_FILE2 while ( <$file2FH> ) { next unless m{$rxFile1}; print; } Here's the output. 123456 foo 123456 bar 123457 foobar
The above code matches the first entry of each line in file2 with the entries of file1. How can I extract those lines in file2 where the entries of file1 appears anywhere in the line?

Replies are listed 'Best First'.
Re: find partially matching lines in a file
by moritz (Cardinal) on Oct 07, 2008 at 20:13 UTC
    Just omit the ^ in the regex.

    Update: And if you want it generally work, you should pipe the items of the regex through quotemeta before building the regex.

      Thanks!