Re: need help on shell scripting
From: Ed Morton (morton_at_lsupcaemnt.com)
Date: 05/02/05
- Next message: Kenny McCormack: "Usenet and bike sheds (Was: Re: CRON : Can you send values to CRON and get a result ?)"
- Previous message: Ed Morton: "Re: need help on shell scripting"
- In reply to: Michael Tosch: "Re: need help on shell scripting"
- Next in thread: Robert Katz: "Re: need help on shell scripting"
- Reply: Robert Katz: "Re: need help on shell scripting"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Kenny McCormack: "Usenet and bike sheds (Was: Re: CRON : Can you send values to CRON and get a result ?)"
- Previous message: Ed Morton: "Re: need help on shell scripting"
- In reply to: Michael Tosch: "Re: need help on shell scripting"
- Next in thread: Robert Katz: "Re: need help on shell scripting"
- Reply: Robert Katz: "Re: need help on shell scripting"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|