loop in awk - column max for each column

Hello all,

this should really be easy for you... I need AWK to print column maxima for each column of such input:

Input:
1 2 3 1
2 1 1 3
2 1 1 2

Output should be:

2 2 3 3

This does the sum, but i need max instead:

{   for(i=1; i<=NF; i++)
    sum +=$i }
END {for(i=1; i in sum; i++)
    printf(sum" ")}

Thank you

If you're on Solaris, use nawk or /usr/xpg4/bin/awk:

awk 'END {
  for (i = 0; ++i <= NF;)
    printf "%s", (m (i < NF ? FS : RS))
  }
{ 
  for (i = 0; ++i <= NF;)
    $i > m && m = $i
    }' infile  
1 Like

Thank you! Blagodarya! Worked on gawk like a charm. This i couldn't figure out, but now i see.

$i > m && m = $i

Molya :slight_smile:
You're welcome!