Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Grep logs by start date and end date in different directories

by Discipulus (Monsignor)
on Jan 03, 2018 at 09:02 UTC ( #1206586=note: print w/replies, xml ) Need Help??


in reply to Grep logs by start date and end date in different directories

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1206586]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2018-07-20 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (435 votes). Check out past polls.

    Notices?