Calculate total value from a row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate total value from a row
# 8  
Old 04-25-2013
Calculate total value from a row

Hi Hanson
Code:
`awk '{print $2 $3 $4 $5}' | grep '[0-9]' | awk '{sum+=$1} END {print sum}'`
awk '{print $2 $3 $4 $5}' #Greps the 2nd till 5th column 
grep '[0-9]' #Finds only the numbers
awk '{sum+=$1} END {print sum}' #sums up all the numbers.. Here am bit confused since doesnt print the exact values correctly

Since the line is read from the file each time of while loop will provide a single line.. So from there I tried grepping columns and calculated the sum..

Note: Please correct me if am wrong anywhereSmilie
# 9  
Old 04-25-2013
Code:
$ awk '{print $2 $3 $4 $5}' marks.txt
LIST

englishfrenchchineselatin
34456723
12391056
23902378

"print $2 $3 $4 $5" just "gloms" fields 2-4 together, as you can see above. That's just the way awk works. It does not add the fields. You certainly do not want that "glomming" to happen in this case. If you use commas between the fields, awk will add a space between the output fields, which is some progress:
Code:
$ awk '{print $2, $3, $4, $5}' marks.txt
LIST

english french chinese latin
34 45 67 23
12 39 10 56
23 90 23 78

Here's the last variation, closer to what you want:
Code:
$ awk '{print $2 + $3 + $4 + $5}' marks.txt
0
0
0
169
117
214

# 10  
Old 04-26-2013
Calculate total value from a row

Hi

Thank You..
But why there are zeros..
If those zeros are truncated, then my requirement will be totally met..
Code:
# awk '{print $2+$3+$4+$5}' marks.txt
0
0
0
169
117
214

The marks.txt file contents:
Code:
# cat marks.txt
        MARKS LIST
     2013
Name english french chinese latini total
wer   34         45     67      23
wqa   12         39     10      56
wsy   23         90     23      78

# 11  
Old 04-26-2013
Quote:
But why there are zeros.
Because every line is printed. If all you want is the totals, here is how to modify your code to print the desired result, the lines with five fields:
Code:
$ awk 'NF == 5 {print $2+$3+$4+$5}' marks.txt
169
117
214

This User Gave Thanks to hanson44 For This Post:
# 12  
Old 04-26-2013
Thanks.. Its finally worked fine..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

How to calculate total number of cores on my servers ?

Hi, I want to get total number of cores on my all non-global zones on Solaris 10. I got two methods and both are giving different results. Below link is a script, which tells me that total cores are 8 Mandalika's scratchpad: Oracle Solaris: Show Me the CPU, vCPU, Core Counts and the... (4 Replies)
Discussion started by: ron323232
4 Replies

2. Shell Programming and Scripting

Calculate total memory using free -m

Hi I am trying to calculate memory used by Linux System free -m total used free shared buffers cached Mem: 32109 31010 1099 0 3600 7287 -/+ buffers/cache: 20121 11987 Swap: 10239 1282 8957 Now according to my requirement Im calculating memory using below cmd free -m | awk 'NR==3{printf... (2 Replies)
Discussion started by: sam@sam
2 Replies

3. Shell Programming and Scripting

Calculate the total

Hi All , I have the following script as below , I tried to modify to meet the requirement , could someone help ? very thanks ================================================================================================ while read STR NAME; do Total=0 MyString="$STR" GetData () {... (18 Replies)
Discussion started by: ust3
18 Replies

4. Shell Programming and Scripting

Calculate total of log by hour

Hi, Just wondering, is there anyway I can get the total of logs generated by hours ? Let say I have these logs, Sep 23 04:48:43 hsbcufs: NOTICE: realloccg /: file system full Sep 23 04:48:47 hsbcufs: NOTICE: alloc: /: file system full Sep 23 04:48:51 hsbcufs: NOTICE: realloccg /: file... (14 Replies)
Discussion started by: dehetoxic
14 Replies

5. UNIX for Dummies Questions & Answers

How to calculate the percentage/fraction of each value in a row against the maximum row value?

Hi, For each row in a file, i would like to identify the maximum value and calculate the percentage/fraction of the max for other values in the row. Then, I would like to print a copy of the file where values above a threshold are replaced with "1" and other values are left as "0". In the... (2 Replies)
Discussion started by: auburn
2 Replies

6. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

7. Shell Programming and Scripting

Calculate total sum from a file

The file content is dynamic and using this format: name1 number1 name2 number2 name3 number3 name4 number4 .................... Need a smooth way to calculate the sum of all the numbers in that file (number1 + number2 + number3 + number4........ = total ) (11 Replies)
Discussion started by: TehOne
11 Replies

8. Shell Programming and Scripting

awk script to calculate total

Hi First field is the Record Type. A Record Type 5 can have multiple Record Type 6's before another Record Type 5 appears. I want to calculate the total of fields at position 8-11 on Record type 6 when Record Type 5 has a field at position 11-14 equals to '2222'. then it should delete the lines... (2 Replies)
Discussion started by: appsguy616
2 Replies

9. Shell Programming and Scripting

Awk help needed to calculate total

Hi all, I have a flat file like 10 steven 25 mike 47 Charles 127 Nancy 34 steven 23 mike 67 Charles 7761 Nancy 8 steven 54 mike 88 Charles 1267 Nancy I need to calculate the total of steven and all the members , for this I am using like grep "`sed -n 1p patterns.txt`"... (7 Replies)
Discussion started by: senthilkumar_ak
7 Replies
Login or Register to Ask a Question