how to add integer with expr?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to add integer with expr?
# 8  
Old 04-27-2009
because i am a beginner of shell programming and i haven't doing things by awk yet, so i don't really know how to use it and don't understand it.
# 9  
Old 04-27-2009
Your input consists of records/line where each field is separated from the other by space.
You need to sum up ALL the fields start from the SECOND field of each record/line and then print the FIRST field and the SUM.
Code:
nawk '
  # a set of actions to be taken for every record/line
  {
   # initialize the "sum" to zero for current record/line
   sum=0
  
   # iterate through the fields starting at (i=2) till the maximum number of field (NF)
   # of the current record/line. For every iteration the the value of the currently 
   # iterated field ($i) to the sum (sum+=$i)
   for(i=2;i<=NF;i++) 
      sum+=$i

   # once the iteration loop is complete, print FIRST field and the "sum". 
   # The "," in the "print" statement get evaluated to the value of the OFS
   # OFS - OutputFieldSeparator
   print $1,sum

}' OFS=: myFile
# Assign ":" to OFS and pass the input file "myFile" to be be parsed by the script.

HTH.
# 10  
Old 04-27-2009
Finding an old Bourne Shell to try was fun.
There are better ways to deal with null parameters than the way I show. It is the way suggested in original Bourne manuals.
Ignore 1st line unless that happens to be where you find Bourne shell. Unlike "ksh" the Bourne shell did not put zero in a blank numeric parameter.

[CODE]
Code:
#!/usr/old/bin/sh
A2=0
A3=0
A4=0
TOTAL=0

cat Marks | awk '{print $1,$2,$3,$4}'|while read A1 A2 A3 A4
do
        if [ "${A2}""X" = "X" ]
        then
                A2=0
        fi
        if [ "${A3}""X" = "X" ]
        then
                A3=0
        fi
        if [ "${A4}""X" = "X" ]
        then
                A4=0
        fi
        #
        TOTAL=0
        TOTAL=`expr ${TOTAL} + ${A2}`
        TOTAL=`expr ${TOTAL} + ${A3}`
        TOTAL=`expr ${TOTAL} + ${A4}`
        echo "${A1}:${TOTAL}"
done[/CODE

]

Last edited by methyl; 04-27-2009 at 11:15 AM.. Reason: Typo
# 11  
Old 04-27-2009
Quote:
Originally Posted by methyl
Finding an old Bourne Shell to try was fun.
There are better ways to deal with null parameters than the way I show. It is the way suggested in original Bourne manuals.
Ignore 1st line unless that happens to be where you find Bourne shell. Unlike "ksh" the Bourne shell did not put zero in a blank numeric parameter.

[CODE]
Code:
#!/usr/old/bin/sh
A2=0
A3=0
A4=0
TOTAL=0

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

Thank you for your helping, it is working.Smilie
# 12  
Old 04-27-2009
Quote:
Originally Posted by methyl
Finding an old Bourne Shell to try was fun.
There are better ways to deal with null parameters than the way I show. It is the way suggested in original Bourne manuals.
Ignore 1st line unless that happens to be where you find Bourne shell. Unlike "ksh" the Bourne shell did not put zero in a blank numeric parameter.

[CODE]
Code:
#!/usr/old/bin/sh
A2=0
A3=0
A4=0
TOTAL=0

cat Marks | awk '{print $1,$2,$3,$4}'|while read A1 A2 A3 A4
do
        if [ "${A2}""X" = "X" ]
        then
                A2=0
        fi
        if [ "${A3}""X" = "X" ]
        then
                A3=0
        fi
        if [ "${A4}""X" = "X" ]
        then
                A4=0
        fi
        #
        TOTAL=0
        TOTAL=`expr ${TOTAL} + ${A2}`
        TOTAL=`expr ${TOTAL} + ${A3}`
        TOTAL=`expr ${TOTAL} + ${A4}`
        echo "${A1}:${TOTAL}"
done[/CODE

]
This is not an entirely 'Bourne Shell' solution and the OP doesn't understand 'awk'.
Plus you don't need a 'cat' and 'awk'.
# 13  
Old 04-27-2009
vgersh99 is quite correct.

The "awk" statement is completely surplus because the fields in the records are already delimited with a space character. The "cat" can be replaced with inward redirect ... but I have chosen not to.


Code:
#!/usr/old/bin/sh
A2=0
A3=0
A4=0
TOTAL=0

cat Marks |while read A1 A2 A3 A4
do
        if [ "${A2}""X" = "X" ]
        then
                A2=0
        fi
        if [ "${A3}""X" = "X" ]
        then
                A3=0
        fi
        if [ "${A4}""X" = "X" ]
        then
                A4=0
        fi
        #
        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 02:06 PM.. Reason: Code tags
# 14  
Old 04-28-2009
Code:
while read line;do
 set $line
 name=$1
 sum=0
 shift
 while [ $# -gt 0 ];do
        sum=$(($sum+$1))
        shift
 done
 echo $name" "$sum
done < a.txt

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