- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: expr command help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2008 08:42 PM
04-11-2008 08:42 PM
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
Solved! Go to Solution.
- Tags:
- expr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2008 09:31 PM
04-11-2008 09:31 PM
Re: expr command help
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
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2008 10:22 PM
04-11-2008 10:22 PM
Re: expr command help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2008 10:31 PM
04-11-2008 10:31 PM
Re: expr command help
please clarify further. Maybe you can post an extract of the script in question.
thanks
kind regards
yogeeraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2008 10:58 PM
04-11-2008 10:58 PM
Re: expr command help
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2008 10:59 PM
04-11-2008 10:59 PM
Re: expr command help
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 } '
- Tags:
- while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2008 11:02 PM
04-11-2008 11:02 PM
SolutionDo it all in awk.
awk -v COL=3 '
BEGIN { getline } # skip first line
{ sum+=$COL }
END { print sum / NR } ' p1.txt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2008 11:11 PM
04-11-2008 11:11 PM
Re: expr command help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2008 11:20 PM
04-11-2008 11:20 PM
Re: expr command help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2008 11:30 PM
04-11-2008 11:30 PM
Re: expr command help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2008 11:32 PM
04-11-2008 11:32 PM
Re: expr command help
I typically just use the man page. But a quick google search finds for awk:
http://www.ibm.com/developerworks/library/l-awk1.html
http://www.gnulamp.com/awkexamples.html
http://www.linuxfocus.org/English/September1999/article103.html
http://www.softpanorama.org/Tools/awk.shtml
http://www.vectorsite.net/tsawk_3.html
http://sparky.rice.edu/~hartigan/awk.html
http://www.cs.hmc.edu/qref/awk.html
http://www.gentoo.org/doc/en/articles/l-awk2.xml
http://www.freeos.com/guides/lsst/ch07sec09.html
And the same for sed:
http://student.northpark.edu/pemente/sed/sed1line.txt
http://www.ibm.com/developerworks/linux/library/l-sed1.html
http://www.ibm.com/developerworks/linux/library/l-sed2.html
http://www.grymoire.com/Unix/Sed.html
http://www.gnulamp.com/sed.html
http://www.cs.utk.edu/~vose/c-stuff/sed_tutorial.html
http://people.debian.org/~caiqian/programming/sed/l-sed1.html
http://www.gentoo.org/doc/en/articles/l-sed1.xml
http://www.freeos.com/guides/lsst/ch07sec14.html
http://www.emunix.emich.edu/~khailany/files/sed-examples.htm
There is probably some duplication in the links.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2008 12:39 AM
04-12-2008 12:39 AM
Re: expr command help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2008 05:54 AM
04-12-2008 05:54 AM
Re: expr command help
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...
- Tags:
- dtksh