Tuni
Tuni

Reputation: 129

get maximum value of column

Hi i have a file like this (with n-lines):

0.001  5.4e+08 
0.03  0.08
0.5774  0.1
93  9832

now i want to read the maximum value of each column. i tried awk like this:

awk -v "max=0.2" -v "index=1" 'BEGIN{if (index>max) max=index} END {print max}' file

and asign the output to a variable. Only typing the awk command in the shell gives me this error:

awk: run time error: cannot command line assign to index
    type clash or keyword
    FILENAME="" FNR=0 NR=0

The Problem is that the file has n-lines and i am only looping over certain lines. How can i give awk the line to work on and read out the maximum value of the column of that line?

Upvotes: 0

Views: 1237

Answers (1)

Ed Morton
Ed Morton

Reputation: 203169

index() is the name of an awk function, pick a different variable name like idx.

Also, to print the max value of an "index" you don't need to seed the max with some ad-hoc value, it would just be:

$ awk -v idx=1 'NR==1 || $idx>max{max=$idx} END{print max}' file
93

Upvotes: 5

Related Questions