Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Ignoring "without opening these files", what do you want returned: the name of the files with a matching pattern, or the matching lines from the files?

Version that outputs the name of the files with a matching pattern:

use File::Glob qw( bsd_glob ); # Inputs my @files = bsd_glob('*'); my $re = qr/^source*/; my @matching_files; foreach my $file_name (@files) { if (!open(my $fh, '<', $file_name)) { warn("Unable to open file \"$file_name\": $!\n"); next; } while (<$fh>) { if (/$re/) { push(@matching_files, $file_name); last; } } } # Output print("$_\n") foreach @matching_files;

Version that outputs the line that match the pattern.

use File::Glob qw( bsd_glob ); # Inputs my @files = bsd_glob('*'); my $re = qr/^source*/; my @matching_lines; foreach my $file_name (@files) { if (!open(my $fh, '<', $file_name)) { warn("Unable to open file \"$file_name\": $!\n"); next; } while (<$fh>) { if (/$re/) { push(@matching_lines, $_); } } } # Output print foreach @matching_lines;

A combination:

use File::Glob qw( bsd_glob ); # Inputs my @files = bsd_glob('*'); my $re = qr/^source*/; my @matches; foreach my $file_name (@files) { if (!open(my $fh, '<', $file_name)) { warn("Unable to open file \"$file_name\": $!\n"); next; } while (<$fh>) { if (/$re/) { # Save the file name, the line number and the line content. push(@matches, [ $file_name, $., $_ ]); } } } # Output print($_->[0], ',', $_->[1], ': ', $_->[2]) foreach @matches;

These solutions are memory efficient.
(Returning the results as an iterator would be even more!)


In reply to Re: search for a pattern in file without opening the file (mem efficient) by ikegami
in thread search for a pattern in file without opening the file by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (6)
    As of 2014-08-23 00:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (169 votes), past polls