I have the following logfile. Currently time in india is 07/31/2014 12:33:34 and i have the following content in logfile. I want to display only those entries which contain string 'Exception' within last 3 hours. In this case, it would be the last line only
[7/30/14 4:59:30 Exception Found
[7/30/14 5:18:55 Result is OK
[7/30/14 8:45:22 Exception found
[7/30/14 9:22:26 Exception found
[7/31/14 11:55:66 Exception found
I can get the timestamp in logfile as
awk '/Exception/ {print $1,$2}' trial.txt | sed 's/^.//' which gives below results
7/30/14 4:59:30
7/30/14 8:45:22
7/30/14 9:22:26
7/31/14 11:55:66
And i can get current timestamp as
date +"%m/%d/%Y %T"
07/31/2014 12:33:34
So, how can i compare current timestamp with timestamp in log file and display 'Exception' entries until last 3 hours.
---------- Post updated at 02:40 AM ---------- Previous update was at 02:07 AM ----------
I am trying this command. But it is throwing exception saying syntax error. What am i doing wrong?
awk '/Exception/ if ($1,$2 > $(date +"%m/%d/%Y %T" -d "3 hour ago")) print $1,$2 ' trial.txt | sed 's/^.//'
If I'm reading MadeInGermany's code correctly, it is comparing YY/DD/MM HH:MM:SS instead of YY/MM/DD HH:MM:SS and is printing the more than 3 hours ago timestamps instead of the less than 3 hours old timestamps. As long as we're constructing strings to compare, I don't see the need to include the slashes in the dates and I'm also assuming that the minutes and seconds do have 2 digits with zero fill so I don't have to split the time fields (I just have to use leading 0 to fill an 8 character field to supply missing leading zeroes in the hour). And, I used FS instead of sub() and split() to split the date field.
I think RudiC left out a %S in the date format string, but on a 3 hour window, a difference of up to one minute might not be noticeable in the results.
I think this does what was requested (on systems where the date utility supports this form of -d option processing):
Passing complex commands with ssh is problematic, because there are two shells that evaluate the script: one on the local host and one on the remote host.
Save the script on the local host, and pass it via stdin to the remote shell:
Now i have another logfile where the timestamp in logs are displayed in a different manner as
2014.09.16 05:15:36.354.MST Exception Found
2014.09.16 08:18:36.277.MST Result Ok
2014.09.16 17:10:22.233.MST Exception Found
This should output the result for last line only as Time currently in india is 17:51 and it is the only string Matching "Exception Found" in the last 3 hours.
How to modify the script propsed earlier to do this?
---------- Post updated 09-17-14 at 01:14 AM ---------- Previous update was 09-16-14 at 07:22 AM ----------
Given the help you have already received, why don't you show us what you have tried on your own to solve this slightly different problem?
The UNIX and Linux Forum is not a place to get free programming services every time you decide to change your requirements. We want you to learn from the examples we provide so you can do it yourself the next time.
What exactly is the function of Sprintf function and why is it in the order of D[3],D[1],D[2] instead of D[3],D[2],D[1] . If we are comparing the current timestamp (date +"%y%m%d%H%M" -d"- 3 hour") against the timestamp in logs, after splitting the timestamp in logs, it should be in order of yy/mm/dd as well.
---------- Post updated at 06:39 AM ---------- Previous update was at 06:38 AM ----------
Please ignore the above. I just realized my mistake. It will be D[3],D[1],D[2] only.
---------- Post updated at 07:54 AM ---------- Previous update was at 06:39 AM ----------
I modified the script as below and its working fine for the new File
Congratulations! We are always glad to hear that the people who have come to The UNIX and Linux Forums are learning how to write code that works after getting help with the basics from the volunteers here.
And thank you for posting your working code; you are now one of the volunteers showing others how you solved your problem.