how do you to add numbers incrementally?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how do you to add numbers incrementally?
# 1  
Old 11-01-2004
how do you to add numbers incrementally?

I've refined the filesystem size using awk and directed to a file name.

eg, here's the content in a file called "numbers"

$cat numbers
345
543
23423456
44435
546
.
.

how do you write a script to all these numbers to get the total?

thanks a lot.
# 2  
Old 11-01-2004
sum=0
for i in `cat numbers`
do
((sum=sum+$i))
done

echo "sum=$sum"
# 3  
Old 11-01-2004
bhargav,
thanks for a quick response. that did it.
# 4  
Old 11-01-2004
Also possible in awk...

awk '{m+=$1}END{print m}' numbers
# 5  
Old 11-09-2004
Hi there,

does someone knows about such a simple method to add hexadecimal numbers using awk ?

thanks,
homefp
# 6  
Old 11-09-2004
To sum two numbers....

With GNU awk, yes
Code:
$ awk 'BEGIN { printf "%x\n", 0xa + 0x2 }'
c

Prefix your hexadecimal values with 0x (or 0X), and use %x as a format specifier.


You also could use ksh
Code:
$ var_a="16#a"
$ var_b="16#2"
$ typeset -i16 var_c
$ ((var_c=var_a+var_b))
$ echo $var_c
16#c

Which is what you'd expect in base 16 (A + 2 = C).
# 7  
Old 11-09-2004
thanks,

how then would I replace the following :

awk '{m+=$1}END{print m}' numbers

with hexa numbers in $1 field ?

I need to have it as simple as possible in a shell script.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read files incrementally and search for particular string.

Example I have following requirements where i need to search for particular string from the log files.Files will be archived with number attached end to it and creates a new log file. First Day i will ran at 8:00 AM Filename:a.log1 Wed Aug 24 04:46:34... (1 Reply)
Discussion started by: nareshnani211
1 Replies

2. Shell Programming and Scripting

Splitting a file incrementally

Dear all, I would like to split a file incrementally. My file looks like: $path { $name "path_sparc_ifu_dec_1" ; $transition { "dtu_inst_d" v ; // (in) "U622/Y" ^ ; // (INVX16_LVT) "U870/Y" ^ ; // (AND2X1_LVT) "U873/Y" v ; // (INVX1_LVT) "U872/Y" ^ ; // (NAND3X0_LVT)... (3 Replies)
Discussion started by: jypark22
3 Replies

3. Shell Programming and Scripting

Linux command rsync sending files incrementally

Please, i have a question about rsync command: Here is the command that i have used in my script: rsync -ratlz --rsh="/usr/bin/sshpass ssh -o StrictHostKeyChecking=no" -aAXHltzh --progress --numeric-ids --devices --rsync-path="rsync" $1 $hostname:$1 using this command, i can... (0 Replies)
Discussion started by: chercheur111
0 Replies

4. Shell Programming and Scripting

Increase two numeric variables incrementally together

Hi, I was wondering if someone could help with what is probably a fairly easy problem. I have two variables, i is between 1-5, j is between 11-15 I'd like to produce this: 1_11 2_12 3_13 4_14 5_15 Each number goes up incrementally with the other. But my shoddy code is not... (5 Replies)
Discussion started by: hubleo
5 Replies

5. Shell Programming and Scripting

Using Awk to Add Numbers

echo "0.1 2.0 0.4 2.0 4.3 1.0 6.0 9.0" | awk 'BEGIN {total=0} {total += $1} END {print total}' I want to add the above output from the echo command, but i can't figure this out. The output above always spits out inaccurate numbers. can someone please provide me with a one liner similar to... (4 Replies)
Discussion started by: SkySmart
4 Replies

6. UNIX for Dummies Questions & Answers

how can i add two numbers

hi, i am having one file which looks like the one below: ABC1 *** 1 4 ABC2 *** 7 12 ABC3 *** 0 34 ... (4 Replies)
Discussion started by: kripssmart
4 Replies

7. Shell Programming and Scripting

How to add numbers

Hello. I new to Shell Scripting. I have a file and here is the output of the file. 1.1M 1.1M 3.3M 149K 61K 75K 144K 135K 82K 170K 327K 2.0M 219K 165K (8 Replies)
Discussion started by: email-lalit
8 Replies

8. Shell Programming and Scripting

How to add numbers?

:confused: I have plain text file "tmp" which include a range of numbers(bytes), say like: 123 234 567 2434 2323 213123 How can I add them and display out. should I use AWK, then how? I am a newer in Bourne shell, please give me a hand, thanks a lot (7 Replies)
Discussion started by: pnxi
7 Replies

9. Shell Programming and Scripting

Add some numbers!

im using this command to return the number of links in my directory, grep -c -i -h "href" *html */*html *htm *shtml is there a way of adding these to get the total? Cheers (3 Replies)
Discussion started by: TalkShowHost
3 Replies
Login or Register to Ask a Question