1

I have a file like this ,

19:36:19_19/06/2017 1786
19:38:21_19/06/2017 2138
19:40:22_19/06/2017 2612
19:42:23_19/06/2017 2613
19:44:28_19/06/2017 2613
19:46:32_19/06/2017 2613
19:48:33_19/06/2017 2613
19:50:35_19/06/2017 2613

upto 6827 lines.

I want to find the difference (only data not timing) of 4th and 1st rows if it is greater than 100 then print the first four lines, if not, then compare 5th and 2nd row and so on upto 6000 rows.

Data in 4th row - 1st row (1786-2613) > 100 then, print first 4 lines (including timing). Then continue with comparison of 5th row- 2nd row (2138-2613) > 100 then print rows 2,3,4,5. Likewise I want to compare upto 6000 lines.

I want output like this,

19:36:19_19/06/2017 1786
19:38:21_19/06/2017 2138
19:40:22_19/06/2017 2612
19:42:23_19/06/2017 2613



19:38:21_19/06/2017 2138
19:40:22_19/06/2017 2612
19:42:23_19/06/2017 2613
19:44:28_19/06/2017 2613
  • "I want to find the difference (only data not timing) of 4th and 1st column" Hm, since timing is on the first column, I don't get what you want. – thecarpy Jul 8 '17 at 8:49
3

You can maintain arrays of the most recent 4 key-value pairs, and cycle through them using modulo arithmetic e.g.

awk '{
  v[(NR-1)%4] = $0; k[(NR-1)%4] = $2
}
NR > 3 {
  if ($2 - k[NR%4] > 100) {
    for (i=NR;i<NR+4;i++) print v[i%4]; print ""
  }
}' file 
19:36:19_19/06/2017 1786
19:38:21_19/06/2017 2138
19:40:22_19/06/2017 2612
19:42:23_19/06/2017 2613

19:38:21_19/06/2017 2138
19:40:22_19/06/2017 2612
19:42:23_19/06/2017 2613
19:44:28_19/06/2017 2613
  • It will work.But I want if it is greater than 100 then print those lines ,if it is not greater than 100 then no need to print those lines.Likewise it want to calculate 6000 lines. – gayathri Jul 9 '17 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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