awk is a extremely powerful tool !
i’m always surprised how much you can do with it.
My latest finding is pretty amazing.
Lets say you have a list of numbers , and you want to figure out the bigger number , the lower number , and the average of all the numbers in the list.
xxxxxxxxxx
cat tmp.txt
40
10
25
35
11
2.2
99.5
100.1
1.5
5
you can use the following awk code to print minimum, maximum and average.
xxxxxxxxxx
cat tmp.txt | awk '{if(min==""){min=max=$1}; if($1>max) {max=$1}; if($1<min) {min=$1}; total+=$1; count+=1} END {print "avg " total/count," | max "max," | min " min}'
The output is going to be :
avg 32.93 | max 100.1 | min 1.5
you can also use this code with decimal numbers