Elisia Light
Elisia Light

Reputation: 115

Parse log on Bash - one result per second

I spent several days for this simple issue and can't get solition!

I have a log, and there are several rows per second. I need only one row per second - and it isn't matter, which one.

Example log:

23:59:58 Incoming packet DeviceId:42FF223F23AC 
23:59:58 Incoming packet DeviceId:42FF223F23AC
23:59:58 Incoming packet DeviceId:42FF223F23AC 
23:59:59 Incoming packet DeviceId:42FF223F23AC 
23:59:59 Incoming packet DeviceId:42FF223F23AC 
23:59:59 Incoming packet DeviceId:42FF223F23AC 

I need this:

 23:59:58 Incoming packet DeviceId:42FF223F23AC 
 23:59:59 Incoming packet DeviceId:42FF223F23AC 

I tried cat log | uniq -w8, but it doesn't work

I'm very sorry of my English - it's not my native language. P.S. Solution on another script language (like PHP, Python) is fine too

Upvotes: 1

Views: 40

Answers (2)

M. Becerra
M. Becerra

Reputation: 659

This should do it:

cat inputFile | sort | uniq -w8

or

sort inputFile | uniq -w8

As uniq is only comparing the first 8 characters, which are hh:mm:ss

Upvotes: 1

nos
nos

Reputation: 229058

If the time field is the only thing you need to consider to determine if a line is different from the previous line, you could do it with this oneliner awk:

awk -F ' '  '{if ($1 != time_prev) {print $0;} time_prev=$1}' < yourfile.txt

Upvotes: 0

Related Questions