1

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!

LanceBaynes
  • 3,087
  • 9
  • 29
  • 31

2 Answers2

1

Give this a try:

last=$(sed -n '$s/^\([0-9]\+\).*/\1/p')
awk -F ASDF -v last=$last '$1 >= last - 120'
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
1

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

mkudlacek
  • 1,677
  • 1
  • 11
  • 15
  • 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 08:01