1828471 Members
2957 Online
109978 Solutions
New Discussion

Re: expr command help

 
SOLVED
Go to solution
piyush mathiya
Trusted Contributor

expr command help

Gurus,
I am very new in shell scripting, i am going to create a script which is calculating the average of io utilization. in that script i want to get the average of floating values like.
5.62
2.75
3.41
1.08
2.41
2.69
----

that means total of all valuses / no. of values. i had used SUM=`expr $SUM + $VALUE`, it shows the following error.

expr: Syntax error
expr: An integer value was expected.

what should I do to get the average of floationg value.

Thanks
Piyush Mathiya
12 REPLIES 12
Yogeeraj_1
Honored Contributor

Re: expr command help

hi Piyush Mathiya,

A much easier method is using awk:

$ cat sum.awk
#sum.awk
{ sum+=$COL}
END { print sum }

$ cat values.txt
5.62
2.75
3.41
1.08
2.41
2.69

$ awk -f sum.awk COL=1 < values.txt
17.96
$

hope this helps!
kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
piyush mathiya
Trusted Contributor

Re: expr command help

Thanks Yogeeraj,
I got your concept but i am not geeting how can i write this statment in a script as i had cut that filed from another file. it is do .. while loop and the floating value is stored in "T" variable.

Thanks
Piyush Mathiya
Yogeeraj_1
Honored Contributor

Re: expr command help

hi Piyush,

please clarify further. Maybe you can post an extract of the script in question.

thanks

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
piyush mathiya
Trusted Contributor

Re: expr command help

Yogeeraj,
What I am doing is.. I have four fields in a file called p1.txt, i have cut 3rd field from the file and i want to calculate the average of that column in a variable "sum".
sum=0
cat p1.txt | grep -v "%" | while read line1
do
T=`echo $line1 | tr -s " " | cut -f 3 -d " "`
sum=`expr $sum + $T`
echo $sum
done

where the "T" is floating value.
Dennis Handly
Acclaimed Contributor

Re: expr command help

>i had cut that field from another file. it is do .. while loop and the floating value is stored in "T" variable.

You need to change it to have awk directly read your file and extract that field and average it. Or take your while loop and create a new file or pipe that you pass to awk (which is probably simpler):

while read stuff; do
# some cut stuff
echo $T
done < first-file | awk -v COL=1 '
{ sum+=$COL }
END { print sum / NR } '
Dennis Handly
Acclaimed Contributor
Solution

Re: expr command help

> I have four fields in a file called p1.txt, i have cut 3rd field from the file and i want to calculate the average of that column in a variable "sum".

Do it all in awk.
awk -v COL=3 '
BEGIN { getline } # skip first line
{ sum+=$COL }
END { print sum / NR } ' p1.txt
Yogeeraj_1
Honored Contributor

Re: expr command help

hi again,

I think Dennis "Do it all in awk." would be simpler to implement in your case.

Thank you Dennis for the detailed input.

Good luck!
kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
piyush mathiya
Trusted Contributor

Re: expr command help

Denis & Yogeeraj.
Thanks for your prompt and realy very helpful answers. Still I want a help. anyone have a good document (Examples) of awk & sed commands. so i can read and learn about this two commands.
Problem is resolved. again thanks.!
Regards,
Piyush Mathiya
Yogeeraj_1
Honored Contributor

Re: expr command help

hi Piyush,

Start with "man awk" and "man sed"

Also have a look at the following URLs:

http://www.grymoire.com/Unix/Sed.html
http://www.grymoire.com/Unix/Awk.html

hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
piyush mathiya
Trusted Contributor

Re: expr command help

Thanks,
James R. Ferguson
Acclaimed Contributor

Re: expr command help

Hi:

Most shells only support integer arithmetic as you have found. While I would probably also use 'awk' (or Perl), the Korn93 shell supports floating point arithmetic.

In HP-UX the Korn shell in '/usr/bin/ksh' is the Korn88 variant and this lacks the support you seek.

However, the Korn93 shell lives in '/usr/dt/bin/dtksh'. For example:

# cat ./float
#/usr/dt/bin/dtksh
typeset -F R1
typeset -F3 R2
typeset -E R3
let R1=1/8
echo $R1
let R2=1/8
echo $R2
let R3=1/8
echo $R3

# ./float
.1250000000
.125
0.125

A google search for 'Korn shell' will yield more information.

Regards!

...JRF...