Hi guys, this is a follow up question from previous thread: Re: Grep logs by start date and end date in different directories . As thanos have provided me a very useful basic script which I am very thankful for, I still have some doubts. This is the code currently (which I have modified) from thanos's script:
use strict; use warnings; use Date::Manip; use Data::Dumper; use File::Find::Rule; use IO::Uncompress::Bunzip2 (); use Net::Subnet; my @LogDir = </cygdrive/c/Users/anon/Documents/logs/png1/>; my @LogDir2 = </cygdrive/c/Users/anon/Documents/logs/png2/*/>; #Find relevant files sub get_files { my (@dirs) = @_; @ARGV = (@LogDir, @LogDir2); my @dirss = join "", map {@ARGV . $_} @dirs; my $level = shift // 3; # level to dig into my @files = File::Find::Rule->file() ->name( '*.bz2','*.log' ) #can insert regex too ->maxdepth($level) ->in(@dirss); #print @files; return @files; } #Matches IP address only sub searchForIP { my ($files, $ip) = @_; my @files = @$files; for my $file (@files){ my $filename = $file; my $fh = IO::Uncompress::Bunzip2->new($filename) or die "bunzip2 $filename: $IO::Uncompress::Bunzip2::Bunzip2Error" +; while (<$fh>){ print "$filename:$.:$_" if /$ip/; } } } #This portion contains some code for user input, I will leave this out + cause its not related to my problem my $numberOfDays = $numdays .' days'; my $dateStart = ParseDate("$sdate"); my $dateEnd = DateCalc($dateStart, $numberOfDays); # To find the every day date1 to date2 my @dates =ParseRecur("0:0:0:1:0:0:0","",$dateStart, $dateEnd); my @datesFormatted = map { UnixDate($_, '%Y-%m-%d') } @dates; my @filess = get_files(@datesFormatted); searchForIP(\@filess, $ip);
As you can see, I have multiple directories that contains many log files. As I am running the script from another directory, I would have to join the @LogDir with the @dirs in order to get the full path. An example of @dirs is 2017-12-01/access.log.bz2 so when joined, they become /cygdrive/c/Users/anon/Documents/logs/png1/2017-12-01/access.log.bz2 . I have tried using only 1 directory containing the logfiles and it worked for me, however,it doesn't work when I try putting in more than 1 directory. I am unsure how I can make it work with more than 1 directories. Below is the code when I tried it with only 1 directory (which worked for me):
#!/usr/bin/perl use strict; use warnings; use Date::Manip; use Data::Dumper; use File::Find::Rule; use IO::Uncompress::Bunzip2 (); use Net::Subnet; my $LogDir = "/opt/splunk/httpd/png1/"; #Find relevant files sub get_files { my (@dirs) = @_; my @dirss = join "", map {$LogDir . $_} @dirs; my $level = shift // 3; # level to dig into my @files = File::Find::Rule->file() ->name( '*.bz2' ) #can insert regex too ->maxdepth($level) ->in(@dirss); #print @files; return @files; }
Any response will be greatly appreciated as I have already tried many ways to insert multiple directories but it still doesn't work... I am not sure why. Thank you again

In reply to Process multiple directories 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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.