extract data only after one hour in shell scripting

126 Views Asked by At

I have a huge logfile inculding timestamp varying every minute with following format :

2017-06-16 00:00:22 - Meter_1_L12_15_3_0 state updated to 124.035
2017-06-16 00:01:54 - Meter_1_L12_15_3_0 state updated to 124.041
2017-06-16 00:02:22 - Meter_1_L12_15_3_0 state updated to 124.047
2017-06-16 00:04:09 - Meter_1_L12_15_3_0 state updated to 124.053

I want to extract log data exactly after one hour with the help of shell scripting of format like :

2017-06-16 00:00:22 - Meter_1_L12_15_3_0 state updated to 124.035
2017-06-16 00:59:51 - Meter_1_L12_15_3_0 state updated to 124.391
2017-06-16 01:00:22 - Meter_1_L12_15_3_0 state updated to 124.396
2017-06-16 01:58:22 - Meter_1_L12_15_3_0 state updated to 124.718

Please help me to achieve this task.Thanks

EDIT: Reevanshi commented the following explanation:
I want to extract every first and last entry of every hour like if any hour for example 2:00 am to 3:00 am ,there are 100 entries in between of them then i just want only first and last entry between that timestamp.

2

There are 2 best solutions below

1
Mark Setchell On

Your input and output data don't correspond to each other, but basically, I think you want to output the previous line and the current line whenever the hour changes.

So, if I make the Input Field Separator either of "space" or colon, I can pick up the hour in field 2 ($2), which gives this:

awk -F'[ :]' '{if($2!=hr){hr=$2;print prev;print}} {prev=$0}' YourLogFile
0
Walter A On

When I call the part until the first colon the head, I need something more than the simple

awk -F: '{if (head!=$1) {if (str) print str; print;}
          head=$1;
          str=$0}' input.logfile

This will work for the most part, but will fail when you only have more than one line in the last hour. In that case you want the last line. You shouldn't print the last line when it is already printed, so you need to remember that.

awk -F: '{if (head!=$1) {if (str) print str; print $0;}
          prevhead=head;
          head=$1;
          str=$0};
       END {  if (head==prevhead) print str}' input.logfile