# Linux – manipulates different files and prints their maximum values to a new file

manipulates different files and prints their maximum values to a new file… here is a solution to the problem.

## manipulates different files and prints their maximum values to 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 want to calculate the maximum value of each corresponding entry in these 1000 files.

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

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

Similar to all other entries. For the example above, 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 don’t know what to do?

### Solution

You can use this `awk`:

``````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
``````