1

Here's my file (in reality a few million rows):

1   75
2   188
3   279
4   267
5   100
6   28
7   479
8   325
9   225
10  181

I want to find the maximum of the second column for first 5 rows (rows 1-5) and print that max value but also the corresponding value from the first column. Then move to the next five rows (rows 6-10) and do the same.

The output should be:

3 279
7 479

So far I can calculate the rolling max this way:

awk '{for(i=1;i<=NF;i++) if($i>maxval) maxval=$i;} NR%5==0 { print maxval; maxval= -1}' input.file

However, I was unable to print the respective value from the first column. I tried defining a second variable such as want=$1 and then trying to print maxval, want but without success. I have a vague impression this could be done with arrays, but all my attempts were unsuccessful.

TP

2 Answers 2

0

I don't think that looping over fields is what you want here. Instead just look at the record number (or file record number to allow it to work over multiple files) modulo 5:

$ awk '!((FNR-1)%5) || $2 > max {x = $0; max = $2} !(FNR%5) {print x}' file
3   279
7   479
  • if the FNR is 1,6,... OR $2 is greater than the current running max, save the current row / max

  • if FNR is 5,10,... print the current row / max

3
  • Works like a charm, very impressive; I'd have never thought of using modulo... many thanks! Jun 29, 2018 at 1:44
  • @ThePresident As soon as you think "for every 5th row" you should also think "for row-number modulo 5".
    – Kusalananda
    Jun 29, 2018 at 6:25
  • @Kusalananda Very valuable advice; I'll try to remember that. Jun 29, 2018 at 17:35
0

Not an awk answer, but for those who like shell tools this GNU split method works:

split -l 5 --filter 'sort -n -k 2 | tail -1'  file
3
  • I get : split: illegal option -- - usage: split [-a sufflen] [-b byte_count] [-l line_count] [-p pattern] [file [prefix]] Jun 29, 2018 at 4:59
  • @ThePresident, It seems the --filter option might be a non-standard GNU split innovation. There's no --filter listed in BSD man split.
    – agc
    Jun 29, 2018 at 7:24
  • That's what I came to realize as well. Thanks anyway! Jun 29, 2018 at 17:35

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .