So my current code calculates the end date when given the start date and the number of days user entered for. I have many directories that contains different log files such as access logs, system logs and etc. The directories are named according to date; e.g.,
Directory name: 2017-12-08 Inside this directory: access.log sys.log
What I want to achieve is when user key in the IP address, start date and number of days, it will grep through the logs(that contains the IP keyed in) from the start date to the end date. So for e.g. if user key in 2017-12-08 and 2 days, all logs from 2017-12-08 to 2017-12-10 will be grep and printed. This is my current code
use strict; use warnings; use Time::Piece (); use Time::Seconds; #Ask for IP address print "Enter an IP address to lookup: "; my $ipAddress = <STDIN>; # I moved chomp to a new line to make it more + readable chomp $ipAddress; # Get rid of newline character at the end #Ask for number of days print "Enter no. of days: "; my $numdays = <STDIN>; chomp $numdays; my $dt = Time::Piece->strptime( $sdate, '%Y-%m-%d'); $dt += ONE_DAY * $numdays; my $edate = $dt->strftime('%Y-%m-%d'); if ($numdays == 1){ my $result = `grep -R -E '$ipAddress' $LogDir | grep -E '$ +sdate'`; if ($result){ print $result; }else{ print "No result found from $sdate. Please try changin +g your IP address/start date and try again.\n"; } } elsif($numdays > 1){ my $result = `grep -R -E '$ipAddress' $LogDir | sed -n '/$ +sdate/,/$edate/{/$edate/d; p}'`; if ($result){ print $result; }else{ print "No result found from $sdate. Please try changing yo +ur IP address/start date and try again.\n"; } }
How can I grep through the dates? currently my code only grep the dates in actual logs itself but this is not a good solution as my logs date format are very inconsistent hence I want to grep via the directory name instead. Any help would be greatly appreciated
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |