Multiple number addition


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Multiple number addition
# 1  
Old 01-02-2011
Multiple number addition

Friends ,

Can u tel me how can i have done multiple addition using shell script ?

Expected Output can be :

1
4
6
---------
Total - 11
This User Gave Thanks to meetsubhas For This Post:
# 2  
Old 01-02-2011
This sounds a bit like homework :-)
Code:
#! /bin/bash

while read n; do
    (( t += n ))
    printf "      %6d\n" ${n}
done

echo '      ======'
printf "Total %6d\n" ${t}

To run:
Code:
./scriptname < inputfile

(assuming you've made the script executable and it is your current directory -- ymmv)
This User Gave Thanks to m.d.ludwig For This Post:
# 3  
Old 01-03-2011
Thanks

Thanks it's working fine...........
# 4  
Old 01-05-2011
#! /bin/bash

while read n; do
(( t += n ))
printf " %6d\n" ${n}
done

echo ' ======'
printf "Total %6d\n" ${t}
# 5  
Old 01-05-2011
Code:
awk '{sum+=$1;print}END {print "Total - ", sum}' infile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

2. Shell Programming and Scripting

Checking File record equal to multiple of 70 or nearest number to multiple of 70

Hello, I have a file with below content - Example 3 6 69 139 210 345 395 418 490 492 I would like the result as - Multiple of 70 or nearest number in the file less than the multiple of 70 69 139 (5 Replies)
Discussion started by: Mannu2525
5 Replies

3. Shell Programming and Scripting

Replace column by random number addition

Here is my problem:- I have a file with pipe separated values. CR|20121021|079|ABC|N|DLS|00038|DLS|04750|1330597704|634234|634|0 CR|20121021|079|ABC|N|DLS|00038|DLS|05118|2071690102|354|351|3 CR|20121021|079|ABC|N|DLS|00038|DLS|05140|960051505|1088|1088|0... (4 Replies)
Discussion started by: Yoda
4 Replies

4. Shell Programming and Scripting

Splitting of files - Addition of Page Number in the Trailer

Hi, I need your help on the following Scenario : Consider a file has 650 records and I need to split this file into 4 files having a maximum of 200 records in each of them and also for the first splitted file it should get appended with Page 1 as a trailer( Similarly for the second file, Page... (4 Replies)
Discussion started by: Ravichander
4 Replies

5. Shell Programming and Scripting

Compare multiple files with multiple number of columns

Hi, input file1 abcd 123 198 xyz1:0909090-0909091 ghij 234 999 xyz2:987654:987655 kilo 7890 7990 xyz3:12345-12357 prem 9 112 xyz5:97-1134 input file2 abcd 123 198 xyz1:0909090-0909091 -9.122 0 abed 88 98 xyz1:98989-090808 -1.234 1.345 ghij 234 999 xyz2:987654:987655 -10.87090909 5... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

6. Shell Programming and Scripting

Number of occurance with multiple conditions??

Hi, I the following sample of out put: + 6.07875 10 0 cbr 210 ------- 2 10.0 2.3 1461 19715 - 6.07875 10 0 cbr 210 ------- 2 10.0 2.3 1461 19715 + 6.07875 22 0 cbr 210 ------- 2 22.0 2.9 1301 19716 - 6.07875 22 0 cbr 210 ------- 2 22.0 2.9 1301 19716 r 6.07922 0 1 cbr 210 ------- 1 30.0... (6 Replies)
Discussion started by: ENG_MOHD
6 Replies

7. Shell Programming and Scripting

Multiple String with a number replacement and more..

Hello all, First of all, I could not made up a nice title what explains my problem in short,sorry for that already. I have the next file which contains the following, CREATE:ENTRY:\ DNAME,"referenceId=sondakika30,referenceId=User1,\ referenceId=Company,\ ... (2 Replies)
Discussion started by: sondakika
2 Replies

8. Shell Programming and Scripting

incremental addition of hex decimal number in one field

Hi I want to incremental add hex decimal number to a particula field in file eg: addr =123 dept1=0 addr = 345 dept2 =1 addr2 = 124 dept3 =2 . . . . . . addr3 =567 dept15 =f Is there any command which add... (8 Replies)
Discussion started by: diddi_linux
8 Replies

9. Shell Programming and Scripting

number subtraction of multiple columns

I get the point of number subtraction in one column awk 'NR==1 {n=$1; next}; {n-=$1} END {print n}' inputfile but I cannot figure it out how to do this to multiple columns. awkward. (6 Replies)
Discussion started by: awkward
6 Replies

10. UNIX for Dummies Questions & Answers

Number of multiple logins

How can i get the number of multiple logins of the user those are logged in currently... (1 Reply)
Discussion started by: Ramkum
1 Replies
Login or Register to Ask a Question