<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Calculating Averages</title>
	<atom:link href="http://nixshell.wordpress.com/2007/03/26/calculating-averages/feed/" rel="self" type="application/rss+xml" />
	<link>http://nixshell.wordpress.com/2007/03/26/calculating-averages/</link>
	<description>UNIX / Linux Shell Hints and Tips&#160;&#160;&#160;&#160;&#160;&#160;(a http://steve-parker.org/sh/sh.shtml subproject)</description>
	<lastBuildDate>Sun, 31 Mar 2013 18:54:44 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Jani "robsku" Saksa</title>
		<link>http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5589</link>
		<dc:creator><![CDATA[Jani "robsku" Saksa]]></dc:creator>
		<pubDate>Sat, 25 Feb 2012 01:34:34 +0000</pubDate>
		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5589</guid>
		<description><![CDATA[/bin/sh in Linux is on most distros a symlink to bash (or dash) and when run it actually runs &quot;Bash in POSIX compatibility mode&quot;, which should be like Bourne Shell, but neither Bash nor Dash is good for actually testing if your script runs on real Bourne Shell.

For those wanting to make sure their script is Bourne Shell compatible but you don&#039;t have actual Bourne Shell available, Heirloom Bourne Shell is something you may want to look into - comes in source package, no configure script, you just have to edit very simple modifications to very simple Makefile, compile and install and you have Bourne Shell with NO extensions whatsoever - with that I&#039;ve learned a lot on how much imagination and crazy hacks you may have to achieve to do things that in Bash are quite normal things to do :) It may be fun, I for one like to do some hobby projects &quot;just to see if I can&quot; where I try to do something in Bourne Shell - like transforming script relying on recursive function calls into Bourne Shell which has no function local variables, only script global ones (hint: subprocess inside function can help, of course the things you can do are limited, subprocess can have it&#039;s own variables, but you can&#039;t change variables outside it, he-he).]]></description>
		<content:encoded><![CDATA[<p>/bin/sh in Linux is on most distros a symlink to bash (or dash) and when run it actually runs &#8220;Bash in POSIX compatibility mode&#8221;, which should be like Bourne Shell, but neither Bash nor Dash is good for actually testing if your script runs on real Bourne Shell.</p>
<p>For those wanting to make sure their script is Bourne Shell compatible but you don&#8217;t have actual Bourne Shell available, Heirloom Bourne Shell is something you may want to look into &#8211; comes in source package, no configure script, you just have to edit very simple modifications to very simple Makefile, compile and install and you have Bourne Shell with NO extensions whatsoever &#8211; with that I&#8217;ve learned a lot on how much imagination and crazy hacks you may have to achieve to do things that in Bash are quite normal things to do <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  It may be fun, I for one like to do some hobby projects &#8220;just to see if I can&#8221; where I try to do something in Bourne Shell &#8211; like transforming script relying on recursive function calls into Bourne Shell which has no function local variables, only script global ones (hint: subprocess inside function can help, of course the things you can do are limited, subprocess can have it&#8217;s own variables, but you can&#8217;t change variables outside it, he-he).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: unixshell</title>
		<link>http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5454</link>
		<dc:creator><![CDATA[unixshell]]></dc:creator>
		<pubDate>Fri, 08 Jul 2011 22:22:02 +0000</pubDate>
		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5454</guid>
		<description><![CDATA[Nicely done, delt. Functions and scripts can easily replace each other too, which is a really nice feature: $1 $2 $#, etc all work just as well for a function as for a script.

PS. There&#039;s no need for &#039;-p &quot;&quot;&#039;, it should default to a blank prompt]]></description>
		<content:encoded><![CDATA[<p>Nicely done, delt. Functions and scripts can easily replace each other too, which is a really nice feature: $1 $2 $#, etc all work just as well for a function as for a script.</p>
<p>PS. There&#8217;s no need for &#8216;-p &#8220;&#8221;&#8216;, it should default to a blank prompt</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: delt</title>
		<link>http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5453</link>
		<dc:creator><![CDATA[delt]]></dc:creator>
		<pubDate>Fri, 08 Jul 2011 19:59:12 +0000</pubDate>
		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5453</guid>
		<description><![CDATA[#!/bin/sh
# Copied/pasted directly from: http://nixshell.wordpress.com/2007/03/26/calculating-averages/
# Then modified a little - mainly for taking numbers directly from the command line.  - delt.

n=0     # n being the number of (valid) data provided
sum=0   # sum being the running total of all data

# &quot;Note that by using ^D (aka &quot;EOF&quot;) to quit, this script will work just as well blah blah&quot;
# -- Thanks, so i can make a function out of this and pipe data directly into it =)

function calc_avg() {
  while read -p &quot;&quot; x; do
    sum=`expr $sum + $x` &amp;&amp; n=$[n+1]  # &quot;expr&quot; return status indicates if valid integer or not
  done

  # ok, finished adding, now calculate the average.
  echo &quot;scale=2;$sum/$n&quot; &#124; bc
}

# like said above, just pipe $* args through calc_avg with a newline between each one.
echo $* &#124; tr &#039; &#039; &#039;\n&#039; &#124; calc_avg


### TODO: write a version that accepts floating point numbers as arguments ###]]></description>
		<content:encoded><![CDATA[<p>#!/bin/sh<br />
# Copied/pasted directly from: <a href="http://nixshell.wordpress.com/2007/03/26/calculating-averages/" rel="nofollow">http://nixshell.wordpress.com/2007/03/26/calculating-averages/</a><br />
# Then modified a little &#8211; mainly for taking numbers directly from the command line.  &#8211; delt.</p>
<p>n=0     # n being the number of (valid) data provided<br />
sum=0   # sum being the running total of all data</p>
<p># &#8220;Note that by using ^D (aka &#8220;EOF&#8221;) to quit, this script will work just as well blah blah&#8221;<br />
# &#8212; Thanks, so i can make a function out of this and pipe data directly into it =)</p>
<p>function calc_avg() {<br />
  while read -p &#8220;&#8221; x; do<br />
    sum=`expr $sum + $x` &amp;&amp; n=$[n+1]  # &#8220;expr&#8221; return status indicates if valid integer or not<br />
  done</p>
<p>  # ok, finished adding, now calculate the average.<br />
  echo &#8220;scale=2;$sum/$n&#8221; | bc<br />
}</p>
<p># like said above, just pipe $* args through calc_avg with a newline between each one.<br />
echo $* | tr &#8216; &#8216; &#8216;\n&#8217; | calc_avg</p>
<p>### TODO: write a version that accepts floating point numbers as arguments ###</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 2010 in review &#171; *nix Shell</title>
		<link>http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5381</link>
		<dc:creator><![CDATA[2010 in review &#171; *nix Shell]]></dc:creator>
		<pubDate>Sun, 02 Jan 2011 12:13:03 +0000</pubDate>
		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5381</guid>
		<description><![CDATA[[...] Calculating Averages March 20077 comments [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Calculating Averages March 20077 comments [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: unixshell</title>
		<link>http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5296</link>
		<dc:creator><![CDATA[unixshell]]></dc:creator>
		<pubDate>Fri, 19 Mar 2010 15:56:22 +0000</pubDate>
		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5296</guid>
		<description><![CDATA[Thanks for that, slevin. I&#039;m sure that there are lots of ways of doing it with awk, perl, and many other languages.]]></description>
		<content:encoded><![CDATA[<p>Thanks for that, slevin. I&#8217;m sure that there are lots of ways of doing it with awk, perl, and many other languages.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: slevin</title>
		<link>http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5295</link>
		<dc:creator><![CDATA[slevin]]></dc:creator>
		<pubDate>Fri, 19 Mar 2010 11:42:49 +0000</pubDate>
		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5295</guid>
		<description><![CDATA[hi,

what about

$ awk &#039;{ sum += $1} END {print sum/NR}&#039; /path/to/data.txt]]></description>
		<content:encoded><![CDATA[<p>hi,</p>
<p>what about</p>
<p>$ awk &#8216;{ sum += $1} END {print sum/NR}&#8217; /path/to/data.txt</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Engineered Solution &#187; HTTP Compression</title>
		<link>http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5236</link>
		<dc:creator><![CDATA[Engineered Solution &#187; HTTP Compression]]></dc:creator>
		<pubDate>Fri, 20 Feb 2009 05:36:47 +0000</pubDate>
		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5236</guid>
		<description><![CDATA[[...] Link: http://www.howtoforge.com/apache2_mod_deflate Link: http://nixshell.wordpress.com/2007/03/26/calculating-averages/ [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Link: <a href="http://www.howtoforge.com/apache2_mod_deflate" rel="nofollow">http://www.howtoforge.com/apache2_mod_deflate</a> Link: <a href="http://nixshell.wordpress.com/2007/03/26/calculating-averages/" rel="nofollow">http://nixshell.wordpress.com/2007/03/26/calculating-averages/</a> [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: unixshell</title>
		<link>http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5202</link>
		<dc:creator><![CDATA[unixshell]]></dc:creator>
		<pubDate>Wed, 16 Jul 2008 00:09:55 +0000</pubDate>
		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5202</guid>
		<description><![CDATA[Thanks Bruce. There are lots of things that Bash can do that Bourne can&#039;t do.

These days, most *nix boxes have Bash available, but /bin/sh still points to the Bourne shell. Indeed, Debian is going back from /bin/sh being bash, to dash. Ubuntu has already replaced /bin/sh with dash.

http://release.debian.org/lenny/goals.txt

Steve]]></description>
		<content:encoded><![CDATA[<p>Thanks Bruce. There are lots of things that Bash can do that Bourne can&#8217;t do.</p>
<p>These days, most *nix boxes have Bash available, but /bin/sh still points to the Bourne shell. Indeed, Debian is going back from /bin/sh being bash, to dash. Ubuntu has already replaced /bin/sh with dash.</p>
<p><a href="http://release.debian.org/lenny/goals.txt" rel="nofollow">http://release.debian.org/lenny/goals.txt</a></p>
<p>Steve</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bruce</title>
		<link>http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5201</link>
		<dc:creator><![CDATA[Bruce]]></dc:creator>
		<pubDate>Tue, 15 Jul 2008 23:59:23 +0000</pubDate>
		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-5201</guid>
		<description><![CDATA[I ran this script on 10&#039;s of thousands of numbers and it takes a *long* time.  That&#039;s because you spawn an expr not once but twice for every number you average.  Here is the script with your expr statement changed to native bash arithmetic expansion.

#!/bin/sh
n=0
sum=0
while read  x
do
  sum=$(( $sum + $x ))
  if [ &quot;$?&quot; -eq &quot;0&quot; ]; then
    (( n += 1 ))
  fi
done
echo &quot;$(echo &quot;scale=2;$sum/$n&quot; &#124; bc) $n&quot;

The last command, bc, is changed a bit to also show the number of elements in the average.

Time to average 10000 items:
OLD: 25.76s
NEW: 0.44s (440ms)]]></description>
		<content:encoded><![CDATA[<p>I ran this script on 10&#8242;s of thousands of numbers and it takes a *long* time.  That&#8217;s because you spawn an expr not once but twice for every number you average.  Here is the script with your expr statement changed to native bash arithmetic expansion.</p>
<p>#!/bin/sh<br />
n=0<br />
sum=0<br />
while read  x<br />
do<br />
  sum=$(( $sum + $x ))<br />
  if [ "$?" -eq "0" ]; then<br />
    (( n += 1 ))<br />
  fi<br />
done<br />
echo &#8220;$(echo &#8220;scale=2;$sum/$n&#8221; | bc) $n&#8221;</p>
<p>The last command, bc, is changed a bit to also show the number of elements in the average.</p>
<p>Time to average 10000 items:<br />
OLD: 25.76s<br />
NEW: 0.44s (440ms)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: unixshell</title>
		<link>http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-2759</link>
		<dc:creator><![CDATA[unixshell]]></dc:creator>
		<pubDate>Wed, 22 Aug 2007 19:28:40 +0000</pubDate>
		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-2759</guid>
		<description><![CDATA[Unfortunately, expr can only deal with integers]]></description>
		<content:encoded><![CDATA[<p>Unfortunately, expr can only deal with integers</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nir</title>
		<link>http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-1152</link>
		<dc:creator><![CDATA[nir]]></dc:creator>
		<pubDate>Sun, 10 Jun 2007 15:04:17 +0000</pubDate>
		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comment-1152</guid>
		<description><![CDATA[for some reason on my system, expr did not work with floating point numbers. I had to change the script to use bc for the calculation:

#!/bin/sh
n=0
sum=0
while read x
do
        sum=`echo $sum + $x&#124;bc`
        if [ &quot;$?&quot; -eq &quot;0&quot; ]; then
                n=`expr $n + 1`
        fi
done
echo &quot;scale=2;$sum/$n&quot; &#124; bc]]></description>
		<content:encoded><![CDATA[<p>for some reason on my system, expr did not work with floating point numbers. I had to change the script to use bc for the calculation:</p>
<p>#!/bin/sh<br />
n=0<br />
sum=0<br />
while read x<br />
do<br />
        sum=`echo $sum + $x|bc`<br />
        if [ "$?" -eq "0" ]; then<br />
                n=`expr $n + 1`<br />
        fi<br />
done<br />
echo &#8220;scale=2;$sum/$n&#8221; | bc</p>
]]></content:encoded>
	</item>
</channel>
</rss>
