in reply to Grep logs by start date and end date in different directories
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*
|
|---|