Help with addition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with addition
# 1  
Old 06-23-2009
Help with addition

Hi all,

I am getting following output by using commands like sort, uniq and awk to the standard output.

110 d
40 a
59 c
9 b
3 e

Now at the end I would like to add all the numbers in column 1 and display the count of all numbers i.e. (110 + 40 + 59 + 9 + 3).

Also the output may vary everytime i.e sometimes my output would be only two lines like

110 d
40 a

or only three lines
110 d
40 a
3 e
Could any body help me with this please?
# 2  
Old 06-23-2009
Code:
do_your_thing |awk '{T += $1 } END { print T }'

# 3  
Old 06-23-2009
Got it. Thank you
# 4  
Old 06-24-2009
Hi,

Knowing how code up little items in awk is very useful. One place to start learning is Awk - A Tutorial and Introduction - by Bruce Barnett

If you use Linux, there are some numerical utilities available. For example, a recent package to appear in the Debian repositories, num-utils, contains a number of perl scripts to deal with numeric data. They allow options on the control statement to choose groups of data on which to operate. One will sum one or more columns and / or rows: numsum. (However, the package doesn't seem to contain a limiting filter, so I wrote one of my own, nulfilter, in perl, briefly demonstrated last below.)
Code:
#!/usr/bin/env bash

# @(#) s2	Demonstrate numsum, local numfilter.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) numfilter
set -o nounset
echo

FILE=${1-data1}
NUMSUM=/usr/bin/numsum

echo " Data file $FILE:"
cat $FILE

echo
echo " Results, sums of columns 1 and 3:"
$NUMSUM -c -x1,3 $FILE

echo
echo " Filter to include only values 30-100 in column 1, sum:"
numfilter -limits 30,100 -- $FILE |
$NUMSUM

exit 0

producing:
Code:
% ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
numfilter (local) 1.1

 Data file data1:
110 d 1.1
40 a 2.2
59 c 3.3
9 b 4.4
3 e 5.5

 Results, sums of columns 1 and 3:
221 16.5

 Filter to include only values 30-100 in column 1, sum:
99

If you do not use Linux, the utilities are available at: num-utils homepage

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Array value addition

Hello all I have a statement : ARRAY_MOUNT_POINT_NAME=`df -h | awk '{print $6}'| head -`expr $i+2` |tail -1` when the value of i=0 , I want the head argument to be at -2 . Using the expr statement isnt working. Help ! (5 Replies)
Discussion started by: Junaid Subhani
5 Replies

2. UNIX for Dummies Questions & Answers

Limitation in addition

whats wrong with this addition? Whats the maximum number of digits can be handled? pandeeswaran@ubuntu:~/Downloads$ const=201234454654768979799999 pandeeswaran@ubuntu:~/Downloads$ let new+=const pandeeswaran@ubuntu:~/Downloads$ echo $new -2152890657037557890 pandeeswaran@ubuntu:~/Downloads$ (4 Replies)
Discussion started by: pandeesh
4 Replies

3. Shell Programming and Scripting

addition of two numbers

by the script, two files Q1 and Q2 will be generated on the system. Q1 will contain an integer number and Q2 also contain an integer number. i would like to add those numbers and put into new file. excerpt from my script 22 subcount=`echo $dir/Q$qid.txt` + `echo $dir/Q$qid.txt` 23 echo... (1 Reply)
Discussion started by: lookinginfo
1 Replies

4. Shell Programming and Scripting

addition of decimal no

a=10.00 pattern=-11.00 b=`echo "$a $pattern" | awk ' printf("%d\n", $1 + $2)'` echo $b not working, also trined bc ,dc but thats not on my m/c. also expr not supporting. any clue? (6 Replies)
Discussion started by: saluja.deepak
6 Replies

5. Shell Programming and Scripting

Awk addition

How would I print out the total amount through awk? I tried using print "Total Amount: " $4+$4; Would I have to do a for loop to get through everything? (2 Replies)
Discussion started by: Boltftw
2 Replies

6. Shell Programming and Scripting

Substitution/Addition

Hi I need to do the following substitution I have to look for line starting with ABC and add 4 ":" before the first occurence of "+"in that line Input ABC:12:Lambert:C278472:1357:0:0:0:0:2:N::::N:9045123:NAP::+DEF output ABC:12:Lambert:C278472:1357.00:0.00:0:0:0:2:N::::N:9045123:NAP::::::+DEF... (5 Replies)
Discussion started by: mad_man12
5 Replies

7. Shell Programming and Scripting

Addition Of New Fields

Hi, I want to add 3 new fields in the existing file.Please find the example below. input: UID: ABCD UNAME: XYZ Desired Output Tmiestamp: 20101208 UID: ABCD UNAME: XYZ DEPTNO:40 ModifyTImestamp:20101209 If you see the above i have added the 3 columns manually in the output section... (2 Replies)
Discussion started by: Nani7574
2 Replies

8. Shell Programming and Scripting

Addition

Hi all, I am very new to shell programming and trying to learn out the basics. I tried this: $ echo `expr 20 + 30` and it worked. But when i tried this,it does not work. $ a=20 $ b=30 $ echo `expr a + b` The error is: expr: non-numeric argument I cant understand why its... (3 Replies)
Discussion started by: gautamshaw
3 Replies

9. UNIX for Dummies Questions & Answers

addition in sh shell

I have to create un counter and I am unable to do an additition: #!/bin/sh count=$1 while ] do echo $count $count=$count+$1 done (1 Reply)
Discussion started by: cfg
1 Replies

10. Shell Programming and Scripting

addition

Hi all, I am new to perl. I need help adding bunch of numbers. I have a file look like this: 1 1 2 1 2 3 1 2 3 4 1 (2 Replies)
Discussion started by: email-lalit
2 Replies
Login or Register to Ask a Question