Manipulate n different files and print their maximum value in a new file

I have 1000 data files -

F1.dat, F2.dat, ....., F1000.dat

F1.dat      F2.dat       F3.dat        F4.dat
2  3  3    3  4  3      3  4  4       2  3  4
3  4  5    2  6  3      4  5  3       4  5  6
5  6  3    2  1  4      2  3  4       0  9  3
2  3  3    1  0  9      1  1  4       3  4  3
.......    ......       ........      .......

      

I would like to calculate the maximum values โ€‹โ€‹of each matching record from these 1000 files.

I mean the first entry (say a11) of my new file will be

new file[a11] = Max(F1[a11], F2[a11], ..., F1000[a11])

      

All other entries are similar. For the above example, the output looks like -

newfile.dat
3   4    4 (i.e. max of 2 3 3 2, max of 3 4 4 3, max 3 3 4 4)
4   6    6
5   9    4
3   4    9
..........

      

I can't think of how to do this?

+3


source to share


1 answer


You can use this one awk

with FNR

:



awk '{
   for (i=1; i<=NF; i++) {
      if ($i > a[FNR,i])
         a[FNR,i]=$i
      }
      rows=FNR;
      cols=NF
}
END {
   for (i=1; i<=rows; i++) {
      for (j=1; j<=cols; j++)
         printf "%s ", a[i,j];
      print ""
   }
}' F*.dat
3 4 4
4 6 6
5 9 4
3 4 9

      

+4


source







All Articles