Find minimum and maximum using AWK

Let’s assume you want to find the minimum and/or maximum of a column of numbers in a given file using AWK.

Assume we have an input file foo with f.ex. line number in first column and in the second ($2) and third column ($3 in awk) we have the values of interest.

File: foo

1 2 40
2 3 23
3 15 -12
4 8 -1
5 11 0

Use the following awk commando find minimum and maximum of the columns of interest:

awk ‘NR == 1 {max=$2 ; min=$2} $2 >= max {max = $2} $2 <= min {min = $2} END { print "Min: "min,"Max: "max }' foo

or

awk ‘BEGIN{ max = -999999999 ; min = 9999999999 } $2 > max {max = $2} $2 < min {min = $2} END{ print "Min: ",min, "Max: ",max }' foo

The result will be for column 2:

Min: 2 Max: 15

and for column 3:

Min: -12 Max: 40

Standard deviation

Here you may see how to calculate the standard deviation using AWK.

Average or arithmetic mean

Here you may see how to calculate the average or arithmetic mean using AWK.

Calculate standard deviation using AWK

The standard deviation ? (sigma) is the square root of the average value of (X – ?)2.

In the case where X takes random values from a finite data set x1, x2, …, xN, with each value having the same probability, the standard deviation is

  where mu

Assume we have an input file foo with f.ex. line number in first column and in the second column ($2 in awk) we have the values of interest.

File: foo

1 2
2 3
3 6
4 8
5 11

Use the one of the following awk commandos to calculate the standard deviation

awk ‘{sum+=$2; array[NR]=$2} END {for(x=1;x<=NR;x++){sumsq+=((array[x]-(sum/NR))^2);}print sqrt(sumsq/NR)}’ foo

awk ‘{sum+=$2;sumsq+=$2*$2} END {print sqrt(sumsq/NR – (sum/NR)^2)}’ foo

The result is

3.28634

Average

Here you may find how to calculate the average or arithmetic mean using AWK.

Minimum and maximum

Here you may find how to calculate the minimum and maximum values using AWK.

Calculate average using AWK

The average or arithmetic mean is given as

Assume we have an input file foo with f.ex. line number in first column and in the second column ($2 in awk) we have the values of interest.

File: foo

1 2
2 3
3 6
4 8
5 11

Use the following awk commando to calculate average or arithmetic mean

awk ‘{s+=$2} END{print “Sum: “s, “\nNumber of lines: “NR, “\nAverage: “s/(NR)}’ foo

The result will be:

Sum: 30
Number of lines: 5
Average: 6.0

Standard deviation

Here you may see how to calculate the standard deviation using AWK.

Minimum and maximum

Here you may find how to calculate the minimum and maximum values using AWK.

Merge two files using AWK

We may use AWK to easily join two files. Assume we have input files foo and foo2

Content of foo

OSL 59.9 10.9
TRD 62.7 11.8

… and foo2

OSL 25 m
TRD 36 m

Join the two files with AWK commando

awk ‘NR==FNR{a[FNR]=$0;next} {print a[FNR],$2,$3}’ foo foo2

or

awk ‘{str=$2;str2=$3; getline < "foo";printf "%3s %3.1f %3.1f %2f %1s\n", $1,$2,$3,str,str2}' foo2

Output will be:

OSL 59.9 10.9 25 m
TRD 62.7 11.8 36 m