conversion to integer not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting conversion to integer not working
# 1  
Old 03-21-2005
conversion to integer not working

Hello..

I have a file containg as below:
Quote:
8.5M
44M
16M
55M
3.0M
45M
12M
164M
My need is to sum the values...
but the error is showing as interger conversion..so i put int() but again not working..please help..here is my code

Quote:
sum=0
while read line
do

expr sum=$sum + int("`echo $line|awk -F'M' '{print $1}' `")
done < test123

Thanks in advance

esham
# 2  
Old 03-21-2005
here is a simple way to do it,
sed 's/M$//' filename | awk '{x += $0} END {print "total: " x "M"}'

Last edited by tanku; 03-21-2005 at 09:51 AM..
# 3  
Old 03-21-2005
Thank You...
that worked excellent... Smilie Smilie Smilie

If you dont mind ,can you explain the usage
sed 's/M$//' filename |

how sed gives the output..
is it one bt one or at a straigt the whole result...
# 4  
Old 03-21-2005
the 's/M$//' replaces the M at the end of the line ($) with nothing then pipe to awk command
# 5  
Old 03-21-2005
for replacing we have to give /g at the end..
or is that not needed..
that made me the confusion..

Smilie
# 6  
Old 03-21-2005
if you want to replace more than once you could add the 'g', but it this case there is only one end of line ($) per line so that was not necessary to add g unless you want to replace a lot of 'M's on the same line
# 7  
Old 03-21-2005
fine got it..

Thanks for the detailed explanation...

Regards
Esham
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Disk Space Utilization in HTML format working in one environment and not working on the other

Hi Team, I have written the shell script which returns the result of the disk space filesystems which has crossed the threshold limit in HTML Format. Below mentioned is the script which worked perfectly on QA system. df -h | awk -v host=`hostname` ' BEGIN { print "<table border="4"... (13 Replies)
Discussion started by: Harihsun
13 Replies

2. Shell Programming and Scripting

Working web service call not working with curl

Hello, Newbie here, I have a perfectly well working web service call I can issue from chrome (PC Windows 10) and get the results I want (a dimmer being turned on in Fibaro Home Center 2 at level 40) I am not allowed to post urls but the below works with http and :// and... (3 Replies)
Discussion started by: abigbear
3 Replies

3. Shell Programming and Scripting

Automating pbrun /bin/su not working, whenever manually it is working using putty

I am trying to automate a script where I need to use pbrun /bin/su but for some reason it is not passing thru the pbrun as my code below. . ~/.bash_profile pbrun /bin/su - content c h 1 hpsvn up file path I am executing this from an external .sh file that is pointing to this scripts file... (14 Replies)
Discussion started by: jorgejac
14 Replies

4. Red Hat

Nslookup working but ping not working at windows client

Hi Team we have created a DNS server at RHEL6.2 environment in 10.20.203.x/24 network. Everything is going well on linux client as nslookup, ping by host etc in entire subnet. We are getting problem in windows client as nslookup working as well but not ping. all the firewall is disabled and... (5 Replies)
Discussion started by: boby.kumar
5 Replies

5. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

6. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

7. Shell Programming and Scripting

Convert to Integer

Hi fellows!! i'm doing something which is not working out for me properly which i don't understand why nowdate=`date +%s` echo $nowdate now the problem how to convert a date which is stored in a variable mydate="22/Oct/2011" mydate=`date -d '$mydate' +%s` it gives error... (11 Replies)
Discussion started by: me_newbie
11 Replies

8. Shell Programming and Scripting

Text data string conversion to Integer

Folks Appreciate your help in understanding issue in relation to below. I need to pul uvalue from a file (tmpfile) and compare it with a number to make decision. Using #!/bin/sh contents of tmpfile : Slot uvalue : 0.16 How I am pulling it: unifval=`awk '/uvalue/ {print $4}' tmpfile` ... (1 Reply)
Discussion started by: wndgs
1 Replies

9. Shell Programming and Scripting

C shell integer tests, less than and more than, not working as desired

Hello, I am creating a script that gives me system load info when i start a shell or log in via ssh(in the /etc/profile.d/ folder). I created it successfully for bash and want it to work with c-shell as well. This is where i'm having problems, the integer test in the if sentence does not work... (3 Replies)
Discussion started by: str1fe
3 Replies

10. UNIX for Dummies Questions & Answers

Rouding off an integer

HI I want to round off an integer to the next multiple of 10 in shell script. (i.e.,) 91 should be rounded off to 100 and 90 should be rounded off to 90 It would be very helpful, if you can help me in this. Thanks in advance (4 Replies)
Discussion started by: dayamatrix
4 Replies
Login or Register to Ask a Question