0

I would like to find the maximum value of one huge matrix I have. I do not need any information of the position, only the value. I have written some easy awk commands, but I struggling with this one.

I suppose I can make a variable run from first field to NF and also from the first line to the last comparing each other, but how can I write it in awk?

1
  • Please edit your post to give an example of matrix. Oct 7, 2016 at 13:03

1 Answer 1

4

try

 awk 'NR == 1 {maxi=$1;} 
      {for(i=1;i<=NF;i++) if ($i > maxi) maxi=$i ;} 
      END { print maxi;}' file

where

  • NR==1 clause initialize for first line maxi value to first field.
  • NF is set for each line to number of field
  • I assume your field are blank/tab separated, you may whish to use FS="," or FS=";" in a BEGIN { } clause.
  • the whole command can be put in a single line, I use several line for readability.
1
  • 6
    Better way to intialize maxi with with an actual data point: replace the BEGIN block with FNR==1 {maxi=$1} Oct 7, 2016 at 13:33

You must log in to answer this question.

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