calculating sum of fields in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting calculating sum of fields in a file
# 1  
Old 01-03-2008
calculating sum of fields in a file

Hey,

I have a file and it has only one field. I need to calculate the sum of each filed as total.

For e.g my file is
1
2
3
4
5

I need to calculate the total sum as 15.

Please let me know how i can do it?
# 2  
Old 01-03-2008
Code:
awk '{total+=$0}END{print total}' file

Regards
# 3  
Old 01-03-2008
Code:
nawk '{ sum = sum + $0 } END { print sum }' file

# 4  
Old 01-03-2008
assuming input file is as stated
Code:
# echo `tr '\n' '+' < file`0 | bc
15

# 5  
Old 01-03-2008
Another one:

Code:
echo $(($(paste -sd+ file)))

Use ksh93 (/usr/dt/bin/dtksh on Solaris) or zsh for floating-point arithmetic.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update file with sum of matching fields in another file

In the awk below I am trying to add a penalty to a score to each matching $1 in file2 based on the sum of $3+$4 (variable TL) from file1. Then the $4 value in file1 is divided by TL and multiplied by 100 (this valvue is variable S). Finally, $2 in file2 - S gives the updated $2 result in file2.... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Calculating sum

Hi all, I have the following code in a shell script. Code: if then echo "##" echo "##" echo "##################################" for fn in `find "$1" -maxdepth 1 -iname \*"$2"* -type f` do echo "$fn" ... (2 Replies)
Discussion started by: naveendronavall
2 Replies

3. Shell Programming and Scripting

Sum fields of different files using awk

I'm trying to sum each field of the second column over many different files. For example: file1: file2: 1 5 1 5 2 6 2 4 3 5 3 3 To get: file3 1 10 2 10 3 8 I found answer when there are only 2 files as... (10 Replies)
Discussion started by: rogeriog.em
10 Replies

4. Shell Programming and Scripting

Need help in finding sum for values in 2 different fields

Hi there, I have 2 files in following format cat file_1 Storage Group Name: aaaa HBA UID SP Name SPPort ------- ------- ------ 0 21 Storage Group Name: bbbb HBA UID... (2 Replies)
Discussion started by: jpkumar10
2 Replies

5. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

6. Shell Programming and Scripting

To perform sum aggregation on numeric fields

Hi all, I have following scenario to perform sum aggregation on certain columns Node Allocated_Space Pool_Name CS_Group Utilized Space -------- ---------------- ---------- --------- -------------- bdw1a_lun01 300 bdw_p0 ... (2 Replies)
Discussion started by: ckwan
2 Replies

7. Shell Programming and Scripting

Counting non empty fields and calculating with that number

Hello all, I have a problem with a skript of mine: My input has the following format 1,33296 transcript_id"ENSRNOT00000018629" 0 1,33296 0 0 transcript_id"ENSRNOT00000029014" 0 0,907392 transcript_id"ENSRNOT00000016905" 0,907392 0 transcript_id"ENSRNOT00000053370" 0 0... (0 Replies)
Discussion started by: DerSeb
0 Replies

8. UNIX for Dummies Questions & Answers

CAlculating page file size

hello dont know if this is the correct forum for this post but i have a question. i am revising for a exam on operating systems and i have the question... A system has a 32bit virtual address divided into 2kbyte pages and each entry in the page table is 6 bytes in length. what is the size... (0 Replies)
Discussion started by: Fortune
0 Replies

9. Shell Programming and Scripting

calculating the time difference, when the script was executed and the currenent file

Hi, I has created the shell script in HP_UX 11.23 and using the command, echo $(date +%Y%m%d%H%M%S) > $DIR/alert, placing the time of running the script into a file alert. I want to compare the time in the above file alert with the current time.If difference is more than 5 min, then print the... (7 Replies)
Discussion started by: velocitnitin
7 Replies

10. Shell Programming and Scripting

sort sum fields

HI ALL, i have a problem when i do a sort sum with many fields. Is there a limit for fields? Do you know a solution? thanks in advance. the shell is: # SORT1 SORT1_rcode=777 if ; then echo "USE $DARSEQ/OTPU.FTPEPREC RECORD F,1000 " > $DARPARSRT/TPEKL508.SORT1_$$.srt ... (6 Replies)
Discussion started by: tullo
6 Replies
Login or Register to Ask a Question