Hello Anonymous Monk,

I applied minor modifications on your code, sample bellow:

#!/usr/bin/perl use strict; use warnings; use Net::Subnet; use Date::Manip; use Data::Dumper; use File::Find::Rule; use IO::Uncompress::Bunzip2 (); my @LogDirs = ('/home/tinyos/Monks/TestDir', '/home/tinyos/Monks/TestDir2'); # add more here #Find relevant files sub get_files { my (@dates) = @_; my @dirss; foreach my $logDir (@LogDirs) { foreach my $dateDir (@dates){ push @dirss, join("/", $logDir, $dateDir); } } 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); 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::Bunzip2Er +ror"; 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 $numdays = 2; my $numberOfDays = $numdays .' days'; my $dateStart = ParseDate("01/01/2018"); # change that to the user inp +ut format (d/m/y) 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); print Dumper \@filess; my $ip = '127.0.0.1'; searchForIP(\@filess, $ip); __END__ $ perl test.pl $VAR1 = [ '/home/tinyos/Monks/TestDir/2018-01-01/access.log', '/home/tinyos/Monks/TestDir/2018-01-01/sys.log', '/home/tinyos/Monks/TestDir/2018-01-02/access.log', '/home/tinyos/Monks/TestDir/2018-01-02/sys.log', '/home/tinyos/Monks/TestDir/2018-01-03/access.log', '/home/tinyos/Monks/TestDir/2018-01-03/sys.log', '/home/tinyos/Monks/TestDir2/2018-01-01/access.log', '/home/tinyos/Monks/TestDir2/2018-01-01/sys.log', '/home/tinyos/Monks/TestDir2/2018-01-02/access.log', '/home/tinyos/Monks/TestDir2/2018-01-02/sys.log', '/home/tinyos/Monks/TestDir2/2018-01-03/access.log', '/home/tinyos/Monks/TestDir2/2018-01-03/sys.log' ]; /home/tinyos/Monks/TestDir/2018-01-03/sys.log:1:127.0.0.1 This is insi +dent 1 in 2018-01-05 /home/tinyos/Monks/TestDir/2018-01-03/sys.log:4:127.0.0.1 This is seco +nd insident 4 in 2018-01-05 /home/tinyos/Monks/TestDir2/2018-01-03/sys.log:1:127.0.0.1 This is ins +ident 1 in 2018-01-05 /home/tinyos/Monks/TestDir2/2018-01-03/sys.log:4:127.0.0.1 This is sec +ond insident 4 in 2018-01-05

Since we do not have your input data and samples of your directories, we only guess. Also for example what is the dates that you are checking? For example the format of the directories (dates) is "2018-01-01" or different. Just by assuming all the time we will not be able to assist you fast enough.

Having said that, on the code above I modified the dates. I assume your code was failing to parse the dates because you had wrong format. Always see the expected input to the module and your input. I simplified it to the user input "01/01/2018" format (d/m/y). Based on this modification I assume it will match your directories.

Also observe that your @LogDirmy and @LogDir2 are strings based on your sample of code why you need to use Arrays? I have modified the code above to a sample of different directory paths that you could use.

I hope this sample of code resolves all your problems, alternatively update your question with more data so we can understand where the problem is coming from.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: Process multiple directories by thanos1983
in thread 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.