how to add integer with expr?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to add integer with expr?
# 1  
Old 04-27-2009
how to add integer with expr?

i got a file called Marks
The format of Marks is:

12345678 5 7
23456789 7 9 3

What can i do with a loop, read expr and echo command to produce a new file like below:

12345678:12
23456789:20

and also when we adding fewer than 3 value with expr, we need to change any null value for variable to 0 so expr gets a valid argument. how can i change it?

So how can i produce a new file with the format above? Using Bourne Shel script.
Thank you for your helping!!Smilie

Last edited by mingming88; 04-27-2009 at 10:21 AM..
# 2  
Old 04-27-2009
One way in "ksh" (you don't state which shell). Note there are Posix ways of doing arithmetic in shell without using "expr" and there are ways of reading a file without using "cat". I hope I have understood the sums required because I make 7+9+3=19 .

Code:
#!/bin/ksh
typeset -i A2=0
typeset -i A3=0
typeset -i A4=0
typeset -i TOTAL=0

cat Marks | awk '{print $1,$2,$3,$4}'|while read A1 A2 A3 A4
do
        TOTAL=0
        TOTAL=`expr ${TOTAL} + ${A2}`
        TOTAL=`expr ${TOTAL} + ${A3}`
        TOTAL=`expr ${TOTAL} + ${A4}`
        echo "${A1}:${TOTAL}"
done


Last edited by methyl; 04-27-2009 at 10:31 AM.. Reason: Forgot code tags
# 3  
Old 04-27-2009
i need to use Bourne shell script.... can't use ksh
# 4  
Old 04-27-2009
Code:
nawk '{sum=0;for(i=2;i<=NF;i++) sum+=$i; print $1,sum}' OFS=: myFile

# 5  
Old 04-27-2009
Quote:
Originally Posted by methyl
One way in "ksh" (you don't state which shell). Note there are Posix ways of doing arithmetic in shell without using "expr" and there are ways of reading a file without using "cat". I hope I have understood the sums required because I make 7+9+3=19 .

#!/bin/ksh
typeset -i A2=0
typeset -i A3=0
typeset -i A4=0
typeset -i TOTAL=0

cat Marks | awk '{print $1,$2,$3,$4}'|while read A1 A2 A3 A4
do
TOTAL=0
TOTAL=`expr ${TOTAL} + ${A2}`
TOTAL=`expr ${TOTAL} + ${A3}`
TOTAL=`expr ${TOTAL} + ${A4}`
echo "${A1}:${TOTAL}"
done
Thank you for helping
# 6  
Old 04-27-2009
i need to do it with a loop, read, expr and echo command....
# 7  
Old 04-27-2009
Quote:
Originally Posted by mingming88
i need to do it with a loop, read, expr and echo command....
Could you please explain why?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk to add/subtract an integer to/from each entry in columns?

---------- Post updated at 01:58 PM ---------- Previous update was at 01:48 PM ---------- For some reason my question is not getting printed. Here are the details: Greetings. I would like to add/subtact an integer to/from two columns of integers. I feel like this should be easy using awk... (3 Replies)
Discussion started by: Twinklefingers
3 Replies

2. Shell Programming and Scripting

Expr: non-integer argument

This is my code.... It works correct, but does not work with 4 and 5. My program is about finding average. so when i run 4 5 it gives me error "expr: non-integer argument". But when i say sh average 45 67 it works. Whats wrong?how to fix it? sum=0 n=0 if then for i in $* do if ... (2 Replies)
Discussion started by: Natalie
2 Replies

3. Shell Programming and Scripting

Getting error in bash script; expr $a + 1: integer expression expected

Hi, I am new to shell/bash script. I am trying to run below script #!/bin/bash a=0 b=10 if then echo "a is equal to be" else echo "a is not equal to be" fi MAX=10 while do echo $a a='expr $a + 1' done (1 Reply)
Discussion started by: Mallikgm
1 Replies

4. Shell Programming and Scripting

expr: An integer value was expected

Hi, I am trying to execute a simple script as below to compare a value from a file and copy that line based on a condition. while read line do code_check = `expr substr "$line" 6 1` if ; then echo "${line}" >> /temp/bill/push_updated.dat else echo "line ignored" fi done <... (8 Replies)
Discussion started by: ramkiran77
8 Replies

5. Shell Programming and Scripting

Non-integer argument in expr

i wrote this simple shell script #!/bin/bash read N1 read N2 expr $N1 + $N2 it work fine in bash and i add it on xinetd for some test but when i try to use in with telnet i got this error : ehsan@debian:~$ telnet 192.168.1.4 1234 Trying 192.168.1.4... Connected to 192.168.1.4.... (14 Replies)
Discussion started by: niasha
14 Replies

6. Shell Programming and Scripting

how to convert string to an integer and how to do calculations like add.,sub.,mult. on it

How to convert string into an integer or number For example : % set tim = `date` % echo $tim Tue Feb 22 16:25:08 IST 2011 here How to increment time by 10 hrs like 16+10 , here 16 is a string in date cmd. .. how to convert 16 to an integer and added to a another nimber ? Thanks... (3 Replies)
Discussion started by: sbhamidi
3 Replies

7. Shell Programming and Scripting

using expr command to add value from two file

How to add two number from two different file using expr command ?? (3 Replies)
Discussion started by: aliahsan81
3 Replies

8. Shell Programming and Scripting

expr: Integer argument too large

Hi all, In KSH, I have got an error message like, "expr: Integer argument too large" I received this error message when I mutiply two large values and displaying the resultant output. Is there any other altenative way to go with too large values? Kindly let me know asap... Thanks in... (12 Replies)
Discussion started by: iamgeethuj
12 Replies

9. UNIX for Dummies Questions & Answers

Add comma to integer using AWK

Srr for being pain her let say i have a data in a file like this 1@1000 2@2000 4@4000 5@7770 6@8998 7@80008 i am a newbie in Unix i need to add a comma to integer using AWK function. for example, 1,000 or 80,008 how can i do that ps. i'm using bash shell (1 Reply)
Discussion started by: Nutter
1 Replies

10. Shell Programming and Scripting

Help: How do I ADD non-integer (decimal) values?

I am trying to create a script that will read from a file two non-integer values (decimals) and add those values together. For example, I want to add 1.51 and -2.37 together and get the sum. Any ideas? Thanks! (2 Replies)
Discussion started by: limshady411
2 Replies
Login or Register to Ask a Question