Re: need help on shell scripting

From: Ed Morton (morton_at_lsupcaemnt.com)
Date: 05/02/05


Date: Mon, 02 May 2005 08:54:03 -0500


Michael Tosch wrote:

> Jitu wrote:
>
>> Hi,
>> Can I achieve some complex programing through scripts.
>> I have a file with three columns all colon seperated.
>> the columns are , name, salary, date of join and date of birth.
>> I need to pick all the rows where the salary is less than 4000 and then
>> get the average of the salary that is less than 4000.
>> Please advice if this is some thing that is possible through shell
>> scripts. I am a C/C++ guy but want to use the scripts if its possible
>>
>> Thanks in advance
>> Jitu
>>
>
> This will avoid an overflow:
>
> #!/bin/sh
> awk -F: '$2<4000{av=(cnt*av+$2)/++cnt;print}END{print "average:",av}' file
>

Good point. You also need to init av to avoid a blank in that case:

awk -F: -vav=0 '$2<4000{av=(cnt*av+$2)/++cnt;print}END{print
"average:",av}' file

though I suspect that introduces more rounding error than this:

awk -F: -vav=0 '$2<4000{s+=$2;av=s/++cnt;print}END{print "average:",av}'
file

Regards,
        Ed.



Relevant Pages

  • Re: need help on shell scripting
    ... Jitu wrote: ... > Can I achieve some complex programing through scripts. ... Michael Tosch @ hp: com ...
    (comp.unix.shell)
  • Re: Help with vbs and html
    ... - What OS are you using to run the scripts? ... - Why avoid using WMI? ... > seem to replicate this type of functionality. ...
    (microsoft.public.scripting.vbscript)
  • Re: Pythonic indentation (or: beating a dead horse)
    ... My work since some year is to decrease probability of errors - I'm creating testing tools, environments and scripts, performing integrations tests and so on. ... I don't like unnecessary syntax elements for scripting languages, because they spoil the view for the real problems. ... So I like Ruby's ability to avoid brackets, ... Would "J" encourage people to avoid comments and empty lines to avoid 50% of scrolling? ...
    (comp.lang.ruby)
  • Re: [PSH] get-credential automation
    ... BTW, even if the scripts are just for you, you should consider using the ... ConvertTo-SecureString: Key not valid for use in specified state. ... UserName Passw ... avoid the popup? ...
    (microsoft.public.windows.server.scripting)
  • Re: RFT: bin/106642: [patch] Allow excluding certain files from mergemaster (8)
    ... Doug Barton wrote: ... like this -- so I wish to avoid mergemaster's questions about installing ... Well another question -- is it possible teach somehow mergemaster, that it should never touch passwd, groups, /etc/ttys or bunch of another files? ... About pre-script which you suggested, there is another problem -- I MAY have new scripts in /etc/rc.d, but NEVER change the system's scripts. ...
    (freebsd-current)