LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Date comparison with 'string date having slashes and time zone' in Bash only (http://www.linuxquestions.org/questions/programming-9/date-comparison-with-string-date-having-slashes-and-time-zone-in-bash-only-760531/)

TariqYousaf 10-08-2009 07:27 AM

Date comparison with 'string date having slashes and time zone' in Bash only
 
The problem statement:
I have a standard thttpd web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format [31/Mar/2002:19:30:41 +0200].

I have developed a command line log analyzer that displays different statistics from the log (e.g. 1- IP with most connection attempts, 2- the most common result codes etc.). In fact to view every stat the user will have to give a specific switch to my utility (like -i and -c respectively with the above examples).

Now comes the problem; with every stat the user can ask to limit the results for either 1. previous n hours. or 2. previous n days. I am unable to use the date from the file with format [31/Mar/2002:19:30:41 +0200] and make a comparison with the timestamp of previous n hours or previous n days.

Constraint: I have to attempt this only in bash.

adevi003 10-08-2009 07:36 AM

For the hour you can

cut -d':' -f2

and do simple math to get a start time. For the day you can

cut -d'/' -f1

That would be more tricky as you would have to logic out the rollback to a previous month.

That's the brute force way. Hopefully someone will know a more elegant solution to actually compare a date type structure.

pixellany 10-08-2009 07:37 AM

Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.

The above is our standard homework response......In this case, I think the key thing is to explain what the problem is--including what code you are using and what exactly happens. Also, when you say "only bash", does that mean you are allowed to use standard utilities like AWK and SED?


All times are GMT -5. The time now is 05:35 PM.