hi all, i have the following problem using awk in a script
i want to read the values from a column with real numbers and calculate the mean.the problem is that when i use a statement such as this
num = $4
i cant find a way to convert the variable from string to floating point to perform any operations.i tried the following
sum = num + 0.0
but sum is equal to 0 after this statement, num is not treated as floating point number but as string
any help would be appreciated
edit : i am using awk 3.1.4 on Suse 9.2
kanagias:
hi all, i have the following problem using awk in a script
i want to read the values from a column with real numbers and calculate the mean.the problem is that when i use a statement such as this
num = $4
i cant find a way to convert the variable from string to floating point to perform any operations.i tried the following
sum = num + 0.0
but sum is equal to 0 after this statement, num is not treated as floating point number but as string
any help would be appreciated
edit : i am using awk 3.1.4 on Suse 9.2
what "operation" are you trying to perform?
for example:
echo '1.2' | nawk '{num=$1; print num, num+3.5}'
i just want to do simple mathematical operations.to be more clear here is a test awk script i used
BEGIN{}
{
num = $1
sum += num
}
END {
print sum
}
where column 1 contains the floating point numbers
print sum produces 0...
i tried to add 0.0 to num to force to be treated as number and not as string but that didnt work either
produces a fine floating point number for me - Solaris's nawk:
nawk '{sum +=$1} END{print sum}' <<!
1.2
1.3
1.4
!
neither awk or gawk works for me...
anyway thanks for replying
works under gawk for me....
there must be something strange with your 'data source'
yes you were right.the problem was with the dots (.), for some reason awk recognizes only the comma as a separator of the integral from the fractal part of the number.thanks for the help man
I bet either your awk is not locale-aware OR your locale is not set up correctly.
try setting LC_ALL=C