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

Hello! I'm a little new to Perl, have to take it as a course for my major (which has little to do with Perl?) and I need a bit of help with a program I need to create. I have to scout a series of "log" files to find certain logs that were made during January of 2006. This is what I have so far:

#!/usr/bin/perl use strict; use warnings; my $dir = "."; opendir (my $dh, $dir ) or die "Cannot open dir '$dir' :$!\n"; my @files; while (my $file = readdir($dh) ) { push (@files, $file); } closedir($dh); my @secure_files; @secure_files = grep (/^secure (\. \d{1,2})?$/, @files);

I believe this takes all of the logs I'm looking for specifically and puts them in an array, but to be honest I'm just copying some code my professor sent us and modifying it in a few places (he told us to do this, I'm not cheating I promise). Basically, I'm a little unsure of how to proceed with the rest of the program. I have to take logs that were made in January (which I plan on doing using basic regex) and then putting them into a file labeled secure.200601. Thanks ahead of time, I appreciate the assistance.

Replies are listed 'Best First'.
Re: Need help copying "logs" to another file.
by haukex (Archbishop) on Mar 28, 2017 at 15:19 UTC

    Normally, this isn't a code writing service, but since what you're asking about is fairly basic, I'll give you some skeleton code for going through a set of files and picking lines out of those files. Please take the time to read perlintro and perlrequick (also perlretut), they aren't too long or complicated, and will explain almost everything in the following code. I used glob instead of readdir because it's easier to use. Feel free to use and adapt this code for your purposes - if you run into trouble or more questions, see the Basic debugging checklist, and if you have trouble and would like to ask questions here, please see the guidelines in How do I post a question effectively? and SSCCE.

    #!/usr/bin/env perl use warnings; use strict; my @files = glob '/path/*.log'; my $out_filename = 'output.txt'; open my $out_fh, '>', $out_filename or die "open $out_filename: $!"; for my $filename (@files) { next if $filename =~ /regex to exclude undesired filenames/; open my $fh, '<', $filename or die "open $filename: $!"; while (<$fh>) { next unless /regex to match desired lines/; print {$out_fh} $_; } close $fh; } close $out_fh;

    Note that the code above is doing roughly the same as:

    $ ls /path/*.log | grep -v filename_exclude_pat | xargs grep -h line_include_pat >output.txt
      Note that the code above is doing roughly the same as:

      $ ls /path/*.log | grep -v filename_exclude_pat | xargs grep -h line_include_pat >output.txt

      right, but note also that the above is doing roughly the same as (a bit expensive as it process every line of unneeded files even if skip them):

      perl -lne 'next if $ARGV=~/filename_exclude_pat/;print if $_=~/line +_include_pat/' *.log > output.txt

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

        Good point :-) Less expensive:

        perl -ne 'INIT{@ARGV=grep{!/filename_exclude_pat/}@ARGV} /line_include +_pat/&&print' /path/*.log >output.txt
Re: Need help copying "logs" to another file.
by AnomalousMonk (Archbishop) on Mar 28, 2017 at 18:40 UTC

    You might also think about getting together with perlnovice1900, who seems to be working on a similar problem; two heads are often better than one.


    Give a man a fish:  <%-{-{-{-<