Need an AWK script to calculate the percentage

Hi
I need a awk script to calculate percentage.

I have to pass the pararmeters in to the awk script and calculate the percentage.

Sum = 50
passed = 43
failed = 7

I need to pass these value in to the awk script and calculate the percentage.

Please advice me.

try using a temporary file tmp
write totalnos passed and failed to file tmp with some filed separator

  1. awk ' FILENAME == "tmp" { print "1st percentage is " $2/100*$1
    print "2nd percentage is $3/100*$1 } ' tmp
    rm tmp
sum=50
passed=43
failed=7

awk -v sum=$sum -v passed=$passed -v failed=$failed '
BEGIN {
 printf "Passed: %3.2f%%\n", passed / sum * 100
 printf "Failed: %3.2f%%\n", failed / sum * 100
 exit
}'

Hi cfajohnson

I was not well for the past 4 days ,today only came to work.
Thank you for the script.

the awk script is working fine,if i excuted alone as you said.
I declared the filed 1 filed 2 filed 3 in th shell script and pass the field 1 and filed 2 filed 3 values as a parameter in to the awk script then it's failing.

i decared the values in the shell script like
sum=50
passed=30
failed=20

Script am excuting this awk script like

awk -f -v file1=$sum -v file2=$passed -v file3=$failed > result.dat

then am getting error like:

syntax error The source line is 1.
The error context is
file >>> 1= <<< ($0)
awk: Quitting
The source line is 1.

Please advice on this.

Hi,
if you already using a script which stores
values in variable like sum, passed, and failed.
add these lines
exp1=$sum*100/$failed
echo the failed percentage is 1 expr $exp1 | bc `
similarly do for pass percentage
and be carefule, the characters are not single quotes but the one above tab key.
write back if problem persists

Hi Paresh n doshi
thanks for the logic.

"be carefule, the characters are not single quotes but the one above tab key." am not clear on this pharse.

I excuted the same as you said.

exp1=$sum*100/$failed
echo the failed percentage is 1 expr $exp1 | bc `
am geting ERROR:
syntax error at line 48 : ``'

i belive am missing some thing.
Please advice.

there are two similar looking characters one is ' and another `
use the latter one.
i don't know what it is called; it shares key with tilde (~).
now that should work

What i need to change in this statment?
exp1=$sum*100/$failed
echo the failed percentage is 1 expr $exp1 | bc `

u mean to say the decalration of this sum,failed,passed should be shared with " ` " this symbol?

lready using a script which stores
values in variable like sum, passed, and failed.
add these lines
exp1=$sum*100/$failed
echo the failed percentage is ` expr $exp1 | bc `
similarly do for pass percentage
and be carefule, the characters are not single quotes but the one above tab key.
expr $exp1 | bc should be enclosed between ``
expr is for calculation of expression passed to bc
and the result is echoed on screen.