Find the answer to your Linux question:
Results 1 to 2 of 2
Hello, the log lines start with date and time like this: Jun 16 08:37:18 166.59.86.116 /opt/.... Jun 16 08:38:19 166.59.86.116 /opt/... Jun 16 08:38:19 166.59.86.116 /opt/... Jun 16 08:40:19 166.59.86.116 ...
  1. #1
    Just Joined!
    Join Date
    Jun 2009
    Posts
    1

    grep from a log the lines generated in the last 4 minutes

    Hello,

    the log lines start with date and time like this:
    Jun 16 08:37:18 166.59.86.116 /opt/....
    Jun 16 08:38:19 166.59.86.116 /opt/...
    Jun 16 08:38:19 166.59.86.116 /opt/...
    Jun 16 08:40:19 166.59.86.116 /opt/...

    I need to grep the lines with a certain error and that were generated in the last 4 minutes, e.g. if current time is Jun 16 08:43 I need the lines starting from Jun 16 08:38:19.
    Is that possible with a bash script or perl code?

  2. #2
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    In bash you can determine what the time was 4 minutes ago with the command:

    Code:
    date --date="4 minutes ago"
    The thing to do is format the times so you can do string comparisons. Usually I format the date/time as: yyyymmddhhmmss (which is year, month, day, hour, minute, second). Look and the date manpage on how to format the date/time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...