Oct
20

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… Continue reading »

Feb
25

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 
Assume we have an input file foo with f.ex. line number… Continue reading »

Mar
30

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,… Continue reading »

Mar
30

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… Continue reading »