Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a log file:
http://pastebin.com/raw.php?i=cdUpTqNV

the first column, before the "ASDF" is the same time, as in the given row, but in unix time [i think it's easier to do this with unix time].

I just need the lines what was in the last 2 minutes:
Ending: 07:55:08 - the last time in the logfile
Starting: 07:53:09 - at most 2 minutes before the last log line in the logfile

The output of the pastebined text would be:
1295938389ASDF 01 25 07:53:09 router authpriv.notice dropbear[20673]: password auth succeeded for 'root' from 192.168.1.201:43822
1295938401ASDF 01 25 07:53:21 router daemon.info dnsmasq-dhcp[1140]: DHCPREQUEST(br-lan) 192.168.1.201 01:2c:23:c3:32:f3
1295938401ASDF 01 25 07:53:21 router daemon.info dnsmasq-dhcp[1140]: DHCPACK(br-lan) 192.168.1.201 01:2c:23:c3:32:f3
1295938508ASDF 01 25 07:55:08 router daemon.info dnsmasq-dhcp[1140]: DHCPREQUEST(br-lan) 192.168.1.201 01:2c:23:c3:32:f3
1295938508ASDF 01 25 07:55:08 router daemon.info dnsmasq-dhcp[1140]: DHCPACK(br-lan) 192.168.1.201 01:2c:23:c3:32:f3

How to do this using only minimal *nix tools? [it's an OpenWrt router, no perl :( ]

so, how to do this: output only line(Last log line time - 120sec)?

Thank you!

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

Give this a try:

last=$(sed -n '$s/^\([0-9]\+\).*/\1/p')
awk -F ASDF -v last=$last '$1 >= last - 120'
share|improve this answer
add comment

You can get current timestamp like this:

date +s%

And current timestamp - 2mins for example this way:

date -d @$((`date +%s`-120)) +%s

So you have two integer values and you want to grep lines which starts within this range. There are many ways how to do it (awk, sed,...)

Sed example:

sed -n -e '/start_time_stamp/,/stop_time_stamp/p' log_file

For other ways, try this magnificent tool: http://lmgtfy.com/?q=grep+number+range

share|improve this answer
1  
This won't work if the the last log entry was more than 2 minutes ago. sed won't find times that aren't there (it won't do >= for example). The "many ways to do it" is the point of the OP's question. "Let me Google that for you" isn't helpful at all. –  Dennis Williamson Jan 25 '11 at 8:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.