Getting the last hour from a log.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting the last hour from a log.
# 1  
Old 01-29-2011
Getting the last hour from a log.

I have a log like this:
Code:
Jan 26 13:59:41 server2 ntpdate[634]: step time server 91.189.94.4 offset 0.065456 sec
Jan 26 13:59:41 server2 ntpd[715]: ntpd 4.2.4p8@1.1612-o Fri Aug  6 22:49:54 UTC 2010 (1)
Jan 26 13:59:41 server2 ntpd[729]: precision = 1.000 usec
Jan 26 13:59:41 server2 ntpd[729]: ntp_io: estimated max descriptors: 1024, initial socket boundary: 16
Jan 26 13:59:41 server2 ntpd[729]: Listening on interface #0 wildcard, 0.0.0.0#123 Disabled
Jan 26 13:59:41 server2 ntpd[729]: Listening on interface #1 wildcard, ::#123 Disabled
Jan 26 13:59:41 server2 ntpd[729]: bind() fd 18, family AF_INET6, port 123, scope 2fails: Cannot assign requested address
Jan 26 13:59:41 server2 ntpd[729]: unable to create socket on eth0 (2) for 2002:54ca:8b5a:1:209:6bff:fe85:6ab7#123
Jan 26 13:59:41 server2 ntpd[729]: failed to initialize interface for address 2002:54ca:8b5a:1:209:6bff:fe85:6ab7
Jan 26 13:59:41 server2 ntpd[729]: Listening on interface #3 eth0, fe80::209:6bff:fe85:6ab7#123 Enabled

I need to get the last hour of log and have made a script for it.
It works, but I need some help simplify it.
I do not need NOW, getting to end of file is good enough, but how to do it.
Can I get all in one line (not use $FROM and $NOW)
Maybe speed it up some. Log size is 2270000 lines long and growing. Takes around 3 seconds to complete.

So I tried this
#!/bin/sh
FROM=$(date +%b" "%d" "%H:%M:%S -d "-1 hour")
NOW=$(date +%b" "%d" "%H:%M:%S)
cat /var/log/daemon.log | awk '$0>=from&&$0<=to' from="$FROM" to="$NOW" | wc -l

Last edited by Jotne; 01-29-2011 at 07:56 PM..
# 2  
Old 01-29-2011
Code:
awk '($0 >= from){++n}END{print n}' from="$(LC_TIME=C date +'%b %d %H:%M:%S' -d -1hour)" /var/log/daemon.log

as you seem to want the count of lines in log from 1 hour ago until now.
# 3  
Old 01-30-2011
Thanks
But I what the output and not the count.
I just added wc -l to see how many lines it generated so I not flooded my screen if some was wrong

So I have no all in one line, but would like to remove the "to" and use END
Code:
awk '$0>=from&&$0<=to' from="$(date +%b" "%d" "%H:%M:%S -d -5hour)" to="$(date +%b" "%d" "%H:%M:%S)" /var/log/daemon.log

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Want to get average value for each hour

I want to get CPU average value only (not required user CPU & memory) with each hours on individual date. The sample output is below | | | User |Memory| User | Date | Time |CPU %|CPU % | % |Mem % | 03/02/2015|00:00:00| 24.56| 20.66| 89.75| 63.48|... (13 Replies)
Discussion started by: Saravanan_0074
13 Replies

2. Shell Programming and Scripting

Grep in a log file within a time range (hour)

Hi, im trying to write a grep script that returns me the last inputs added in the last hour in the log file. Literally i have nothing yet but: grep 'Line im looking for' LOGFILE.log | tail -1 this only gives me the last input, but no necessarily from the last hour. Help Please. (4 Replies)
Discussion started by: blacksteel1988
4 Replies

3. Shell Programming and Scripting

Pick the last one hour lines from log matching this pattern.

Hello please help me on this, pick the last one hour lines from the log, which have the prefix time format like this. log message log message i tried to do grep, but that failed. my code grep '(date +)' log_file_path This checking only the current time stamp. How to get the log... (16 Replies)
Discussion started by: santosh2626
16 Replies

4. Shell Programming and Scripting

Pattern from a log within last hour

I want to extract a pattern from a log file within last hour. I am doing grep "pattern" filename | tail -1 It gives the last latest pattern but not within last hour. and if the pattern found in last hour, output success or else failure. (3 Replies)
Discussion started by: santosh2626
3 Replies

5. Shell Programming and Scripting

Write Log per hour

Hi, the follow can add the day time to my Log file My_Log_$(date +%F).log can I add the hour to the end of my log file then my log file will looks like My_Log_07-24:(:confused: (3 Replies)
Discussion started by: Hscript
3 Replies

6. Shell Programming and Scripting

How to get next hour?

Hi guys I want any script to get me next hour For example Nexthour.sh 2013022823 It get me result 2013030100 Thanks a lot , I'm using Solaris 10 (5 Replies)
Discussion started by: teefa
5 Replies

7. Shell Programming and Scripting

How to convert 24 hour time to 12 hour timing?

Hi friends, I want to convert 24 hour timing to 12 hour please help me... my data file looks like this.. 13-Nov-2011 13:27:36 15.32044 72.68502 13-Nov-2011 12:08:31 15.31291 72.69807 16-Nov-2011 01:16:54 15.30844 72.74028 15-Nov-2011 20:09:25 15.35096 ... (13 Replies)
Discussion started by: nex_asp
13 Replies

8. Shell Programming and Scripting

Calculate total of log by hour

Hi, Just wondering, is there anyway I can get the total of logs generated by hours ? Let say I have these logs, Sep 23 04:48:43 hsbcufs: NOTICE: realloccg /: file system full Sep 23 04:48:47 hsbcufs: NOTICE: alloc: /: file system full Sep 23 04:48:51 hsbcufs: NOTICE: realloccg /: file... (14 Replies)
Discussion started by: dehetoxic
14 Replies

9. UNIX for Dummies Questions & Answers

an hour less in 24 hour system

My program: __________________________________ #!/bin/ksh DAY=`date +%y%m%d` H=`date +%H` M=`date +%M` day=`date +%m/%d/%y` let h=$H-1 echo DAY $DAY echo H $H echo M $M echo day $day echo h $h _____________________________________ My result: (3 Replies)
Discussion started by: bobo
3 Replies

10. UNIX for Dummies Questions & Answers

1 Hour less

set DAY=`date +%y%m%d` set H=`date +%H` set M=`date +%M` mailx -s "$H-Mydata" myemail@mail.com<mydata I am looking to set the current hour to have 1 hour less in the subject header: For example: let's say the system time is 8 I want to have "7-Mydata" not "8-Mydata" Can some1... (6 Replies)
Discussion started by: bobo
6 Replies
Login or Register to Ask a Question