Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a log file:

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
1295938401ASDF 01 25 07:53:21 router dnsmasq-dhcp[1140]: DHCPREQUEST(br-lan) 01:2c:23:c3:32:f3
1295938401ASDF 01 25 07:53:21 router dnsmasq-dhcp[1140]: DHCPACK(br-lan) 01:2c:23:c3:32:f3
1295938508ASDF 01 25 07:55:08 router dnsmasq-dhcp[1140]: DHCPREQUEST(br-lan) 01:2c:23:c3:32:f3
1295938508ASDF 01 25 07:55:08 router dnsmasq-dhcp[1140]: DHCPACK(br-lan) 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
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

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:

share|improve this answer
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

Your Answer


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.