Saturday, April 12, 2008

Min,max and average using awk


The report.txt contains the working hours details of employee C1 in a particular week.

$ cat report.txt
Office1:Cl:8:Mon
Office1:Cl:1:Tue
Office2:Cl:5:Wed
Office1:Cl:3:Thu
Office3:Cl:6:fri

Required Output:
------------------
minimum hours:1(Tue)
maximum hours:8(Mon)
average:4.6

Here is the code:
----------------
$ awk 'BEGIN {FS=":"}
min=="" {
min=max=$3 ; minday=maxday=$4
}
{
if ($3 > max) {max = $3; maxday = $4};
if ($3 < min) {min = $3; minday = $4};
total += $3
count += 1
}
END {
print "minimum hours:" min"("minday")";
print "maximum hours:" max"("maxday")";
print "average:" total/count;
}
' report.txt

No comments:

© Jadu Saikia www.UNIXCL.com