# Display the standard deviation of a column of numbers with awk

awk '{delta = \$1 - avg; avg += delta / NR; mean2 += delta * (\$1 - avg); } END { print sqrt(mean2 / NR); }'
This will calculate a running standard deviation in one pass and should never have the possibility for overflow that can happen with other implementations. I suppose there is a potential for underflow in the corner case where the deltas are small or the values themselves are small.

4
2009-09-11 04:46:01

## 2 Alternatives + Submit Alt

• 6
awk '{sum+=\$1; sumsq+=\$1*\$1} END {print sqrt(sumsq/NR - (sum/NR)**2)}' file.dat
· 2009-03-24 21:56:40
• I was concerned that the other scripts didn't use the correct order of operations for the calculation. Variance needs to be squared before being summed, then divided by NR and then square rooted. Not sure if this method is correct but this is what I came up with. I think it works correctly but I'm not totally sure. If anyone could critique it that would be great.

0
awk '{delta=\$1; avg+=\$1/NR;} END {print "stdev = " sqrt(((delta-avg)^2)/NR);}'
· 2016-04-19 08:49:49

### What Others Think

I was the one who commented on the alternate/older system which was failing. Not 100% on how your scheme works, but it appears to get the same result at the stuff I posted. Good job! -- `grep -e '^#BEST' 00111260_Control.ASC | grep 'L1_INT' | cut -d ',' -f 14 | less | awk '{sum+=\$1; array[NR]=\$1} END {for(x=1;x<=NR;x++){sumsq+=((array[x]-(sum/NR))**2);}print sqrt(sumsq/NR)}'` 0.0111318 `grep -e '^#BEST' 00111260_Control.ASC | grep 'L1_INT' | cut -d ',' -f 14 | less | awk '{sum+=\$1; sumsq+=\$1*\$1} END {print sqrt(sumsq/NR - (sum/NR)**2)}' ` 0.0111299 `grep -e '^#BEST' 00111260_Control.ASC | grep 'L1_INT' | cut -d ',' -f 14 | less | awk '{delta = \$1 - avg; avg += delta / NR; mean2 += delta * (\$1 - avg); } END { print sqrt(mean2 / NR); }'` 0.0111318 --
mungewell · 755 weeks and 1 day ago
Thanks. And thank you for testing it on your large data set.
ashawley · 755 weeks and 1 day ago
Since 2008, Perfect House Pug Puppies has been the leading Pug puppy breeder in the US, providing premium Pug puppies for sale. Each puppy exhibits the professionalism and knowledge that have set Perfect House Pug puppies apart from other pug puppy breeders. The professionals at Perfect House Pug Puppies are dedicated about finding their puppies loving, beloved homes where they can enjoy long, healthy lives. A companion that has been produced for quality, health, and temperament that satisfies the breed standard is guaranteed by Perfect House Pug puppies, the pug breeder that has set the benchmark for exceptional customer satisfaction. If you are looking to buy a baby pug, Black and white pugs, Mini pug puppies for sale, Black and tan pug for sale, Teacup pug puppies for sale, Female pug puppies, pug puppies for sale, pug puppies for sale near me, pug puppies for sale under \$500 near me. Pug Puppies for Sale Near Me pugs puppies for sale teacup pugs for sale pug puppies for sale by owner pug puppies ohio PUG PUPPY FOR SALE NEAR ME PUG PUPPIES FOR SALE pug puppies for sale in kentucky Pug Puppies for Sale Under \$500 Near Me pug puppies for sale in texas pug puppies for sale \$200 pugs for sale near me under \$500 pugs for sale under \$400 near me pugs for sale near me puppies for sale near me under \$500 pug puppies for sale under \$1,000 near me pug for sale pug puppies for sale under \$300 Brindle Pug Pitbull Pug Mix Pugs for sale cheap Cheap pug affordable pug puppies for sale near me black pugs for sale near me White Pugs for sale pug dog for sale free pug puppies pug puppies for sale in my area mn pug breeders pug puppies indiana pugs for sale michigan PUG PUPPY ADOPTION Pug puppies for sale Pug puppies for sale near me Pug puppies near me Pug Puppies for Sale Under \$500 Near Me Cute Pug Puppies Black pug puppies Black pug puppies for sale pug puppies for adoption black pug puppies for sale near me chihuahua pug mix puppies how much is a pug puppy teacup pug puppies baby pug puppies baby pug puppies for sale pictures of pug puppies pug puppies for sale in Ohio pug puppies price pug mix puppies teacup pug puppies for sale best food for pug puppy newborn pug puppies pug puppies craigslist pug puppies for sale craigslist adorable pug puppies how much does a pug puppy cost Pitbull pug mix puppies pug pit mix puppy pug puppies for sale \$200 pug puppies for sale in NJ Pug puppies for sale in Wisconsin pug puppy cost pug puppy food royal canin pug puppy royal canin pug puppy food fawn pug puppy pug puppies for sale florida pug puppies for sale in Indiana pug puppies for sale in KY pug puppies for sale in NC pug dog puppy AKC Registered Pug Puppies For sale cheap pug puppies for sale near me cheap pug puppies for sale in California cheap pug puppies for sale in nj Black Pug Puppies for sale pugs puppies for sale
Perfecthomepugs · 69 weeks and 4 days ago
pugpuppies95 · 31 weeks and 4 days ago

### What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

### What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.