Hello and welcome to the wonderful world of Perl!

as general advice avoid to shell out if Perl lets you to do something in it's own way: search afoken's threads about shell to know why and how.

Anyway if you need to build up such list of directories the better, imho, approach is looping different time values going backward. consider the following little snippet:

foreach my $day(0..15){ my @ymd = (localtime(time - 3600*24*$day))[5,4,3]; # build up an + array of just year,month and day, n days backward say join'-',$ymd[0]+1900,(sprintf '%02d',$ymd[1]+1),(sprintf '%02 +d',$ymd[2]); #print them in the format you need } 2018-01-03 2018-01-02 2018-01-01 2017-12-31 2017-12-30 2017-12-29 2017-12-28 2017-12-27 2017-12-26 2017-12-25 2017-12-24 2017-12-23 2017-12-22 2017-12-21 2017-12-20 2017-12-19

With such strings then build up the full path you need then use glob to build up a list of files and finally open them to search your wanted IPs.

If you organize this using subroutines all will be clean and wise! You can the add GetOpt::Long to admit a --foreward option to process in reverse order directories, just @dirs_date = reverse @dirs_date;

If you avoid the shell you can generalize the program to search not only IPs but whatever you want letting the user to enter a custom regex possibly passed in command line as argument.

# untested... foreach my $filepath (@paths){ open my $fh,'<', $filepath or die "unable to open $filepath!"; while (<$fh>){ if($_ =~ /$usr_supplied_regex/){ print "$filepath:$.".$_; } } }

PS consider to sign in to the monastery!

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.

In reply to Re: Grep logs by start date and end date in different directories by Discipulus
in thread Grep logs by start date and end date in different 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.