Hi all, I am a beginner to Perl so please understand.

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


In reply to 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.