LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-07-2012, 04:34 PM   #1
rrank5377
LQ Newbie
 
Registered: Nov 2006
Distribution: Kubuntu 22.10
Posts: 9

Rep: Reputation: 0
Scan for time/date in a log file


Hi everyone,

I'm trying to monitor a log file for a certain string. I'm thinking that I'll be doing a cron job to scan the file every x minutes.

In an attempt to eliminate false positives, I want to parse out the newest part of the file. Each log entry looks something like this:

2012-06-07T12:49:42.342-0700 ErrorMessageHere

I want to find all entries newer than the last scan. I'm going to store the scan time in a file, so at the beginning of the script, I will read that time and compare the values. Any time in the log newer than that time should be what I'm looking at

My problem is this: I don't know how to read and compare the date&time stamp. I'm guessing I'll have to use awk, but I'm at a loss as to how.

Any assistance that can be offered is welcome.
 
Old 06-07-2012, 05:38 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
The timestamp you have is sortable. That is the beauty of ASCII.
2012-06-07T12:49:42.342-0700
(but what is the -0700 bit?)

Why would you have to use awk?
Any language with string comparison will work as expected.

Code:
while read timestamp message; do
   [[ $saved > $timestamp ]] && echo $timestamp $message
done
Alternatively you could take the file position instead, that would be easier as long
as the log does not truncate.

Last edited by bigearsbilly; 06-07-2012 at 05:46 PM.
 
Old 06-08-2012, 09:22 AM   #3
rrank5377
LQ Newbie
 
Registered: Nov 2006
Distribution: Kubuntu 22.10
Posts: 9

Original Poster
Rep: Reputation: 0
Man, did I overthink that!

That while statement did the trick. Thank you very much!
 
Old 06-08-2012, 11:30 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
It's nice to be useful occasionally!
 
Old 06-09-2012, 10:04 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by bigearsbilly View Post
but what is the -0700 bit?
It's the numeric representation of the timezone, ±HHMM compared to UTC.

If you ignore the effect of the timezone on the time stamps, then
Code:
awk -v since="timestamp" '($1 >= since)' log-file
should also work. You could use
Code:
since="$(awk -v since="$since" '($1 >= since) { printf("%s\n", $0) > "/dev/stderr" ; if ($1 > max) max = $1 } END { printf("%s\n", max) }' log-file)"
to output all new entries in log-file to standard error, while also updating the timestamp. If since is initially empty, it will output the entire log file.

Note the >= . It means all entries matching the final timestamp in the last round will be included in the next round, but that is intentional: that way you don't lose an error message that happens in the same millisecond but AFTER you have last read the log file. If you don't want the repeats, and are willing to risk missing an error message if it happens at the same millisecond, then you can use > instead for the comparison.

If you don't want to miss anything, but don't want any repeats either, you need a timestamp and a counter or a hash list (a single since variable, but with two or more words in it separated by whitespace); the counter specifying the number of log lines output for that timestamp, or each hash matching an already output log line at that timestamp. (The latter works more reliably when the log files are rotated.) The awk script gets progressively more complicated, and I'd personally just live with the initial duplicate log line(s).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Log Date/time doesn't change with system time saurabhchokshi Linux - Software 5 07-07-2010 12:27 AM
Adding date and time to Squid access.log jonette20 Linux - General 5 09-21-2009 07:41 AM
How to write function to list file (*.log) follow feature time(date, month, year) phanvinhgiap Programming 2 12-30-2008 08:30 PM
Adding date and time to a log file ltodd2 Linux - General 2 12-17-2008 10:05 AM
how to add date n time in proxy access.log sunlinux Linux - Networking 3 08-23-2007 06:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration