Skip to content

Instantly share code, notes, and snippets.

@karlarao
Forked from jamerfort/avg.awk
Last active September 13, 2018 19:32
Show Gist options
  • Save karlarao/02e0ae9159c908bdf5a13a31ec601e0b to your computer and use it in GitHub Desktop.
Save karlarao/02e0ae9159c908bdf5a13a31ec601e0b to your computer and use it in GitHub Desktop.
Awk scripts for total/average/max/min. All numbers come in on STDIN
#!/usr/bin/awk -f
BEGIN { TOTAL=0 }
{ TOTAL = TOTAL + $1 }
END { print TOTAL/NR }
https://stackoverflow.com/questions/19149731/use-awk-to-find-average-of-a-column
cat awk_minmax2.awk
# usage: ls -l | awk -f awk_minmax2.awk
# check this for min https://gist.github.com/jamerfort/4164638
{
sum+=$5
if (NR==1) {
min=$5
max=$5
min_name=$9
max_name=$9
}
if ($5 > max) {
max=$5
max_name=$9
}
#if ($5 < min) {
# min=$5
# min_name=$9
#}
if( min == "" || $5 < min )
{ min=$5 }
}
END{
print "sum:",sum,"max:",max,"min:",min,"avg:",sum/NR
}
#!/usr/bin/awk -f
{ if( MAX == "" || $1 > MAX ) { MAX=$1 } }
END { print MAX }
#!/usr/bin/awk -f
{ if( MIN == "" || $1 < MIN ) { MIN=$1 } }
END { print MIN }
#!/usr/bin/awk -f
BEGIN { TOTAL=0 }
{ TOTAL = TOTAL + $1 }
END { print TOTAL }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment