in reply to Process multiple directories
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Process multiple directories
by Anonymous Monk on Jan 09, 2018 at 01:04 UTC | |
by thanos1983 (Parson) on Jan 09, 2018 at 10:34 UTC | |
|
Re^2: Process multiple directories
by Anonymous Monk on Jan 09, 2018 at 02:03 UTC | |
by thanos1983 (Parson) on Jan 09, 2018 at 10:31 UTC | |
|
Re^2: Process multiple directories
by dotowwxo (Acolyte) on Jan 09, 2018 at 03:00 UTC | |
by dotowwxo (Acolyte) on Jan 10, 2018 at 06:48 UTC |