Operating System - HP-UX
1834346 Members
2130 Online
110066 Solutions
New Discussion

Re: Shel Script Calculation

 

Shel Script Calculation

Hi,
Please find my shell script :
let Total=0
for i in `cat data1.dat`
do
let Total=$Total+1
done
echo $Total

I have series of integer number stored in data1.dat file.
If the range too huge, my calculation become wrong.
Please help.
Thanks

3 REPLIES 3
Victor Fridyev
Honored Contributor

Re: Shel Script Calculation

Hi,

Try to use awk or perl for calculations

HTH
Entities are not to be multiplied beyond necessity - RTFM
James R. Ferguson
Acclaimed Contributor

Re: Shel Script Calculation

Hi:

You are limited by the shell's 32-bit arithmetic. As noted, by Victor, 'awk' or Perl would provide a solution. Based upon your example, to sum the first field found in your file, do:

# awk 'END{print N};{N+=$1+0}' data1.dat

Regards!

...JRF...

Re: Shel Script Calculation

thanks