Operating System - Linux
1753287 Members
5395 Online
108792 Solutions
New Discussion юеВ

Re: [Q] asking for korn shell, specially summation of float ..

 
Tony, Lim
Frequent Advisor

[Q] asking for korn shell, specially summation of float ..

All..

I have a file contain like this information.

infodba@/home/infodba/temp$ head 1st.rst
KRBPGWDCAD00001 35280 1 50.076 0.14%
KRBPGWDCAD00002 35280 1 0.703 0.00%
KRBPGWDCAD00003 35280 1 2.04 0.01%
KRBPGWDCAD00004 35280 1 8.831 0.03%
...

I would like to sum $4 to one variable.
However, it is float number and I have no idea how it sum to one values.

Could you let me know how the $4 can be summed using "for" command ?

Additionally, I would like to know how $2 (integer) and $4 (float) can be divided ?
4 REPLIES 4
RAC_1
Honored Contributor

Re: [Q] asking for korn shell, specially summation of float ..

Shell can do only integers and that too certain limit. You can use awak here.

awk '{(s+=$4)} END {printf "%d\n",s}' your_file
There is no substitute to HARDWORK
spex
Honored Contributor

Re: [Q] asking for korn shell, specially summation of float ..

Hi,

Sum:
# awk '{(s+=$4)} END {printf "%6.3f\n",s}' 1st.rst

Overall quotient:
# awk '{d+=$2;n+=$4} END {printf "%f\n",(n/d)}' 1st.rst

Quotient by line:
# awk '{printf "%f\n",($4/$2)}' 1st.rst

PCS
Florian Heigl (new acc)
Honored Contributor

Re: [Q] asking for korn shell, specially summation of float ..

typeset -f in ksh93 would work. (dtksh is a ksh93 on HP-UX)

Now if I just would be able to find the ksh93 release notes. (google has only one hit for them, and I don't remember the magic string)
yesterday I stood at the edge. Today I'm one step ahead.
Bill Hassell
Honored Contributor

Re: [Q] asking for korn shell, specially summation of float ..

The Korn shell and POSIX shell do not have floating point arithmetic so you have to use an external program. awk is one method, bc is the other most popular method.


Bill Hassell, sysadmin