Calculate total value from a row


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

HI

I have a file
Code:
# cat marks.txt
        MARKS LIST
               2013
Name english french chinese latin total_marks
wer        34    45     67      23
wqa       12      39     10      56
wsy       23      90     23      78

Now i need to find the total marks of each student using ksh..

Please help..Smilie

Thanks in advance
# 2  
Old 04-23-2013
Any chance that's a homework problem?
# 3  
Old 04-25-2013
Calculate total value from a row

Hi

This is not a homework problem Smilie

I proceeded the following code
Code:
#!/usr/bin/ksh
set -x
TotalLines=`cat marks.txt | wc -l`
Line=4
while [ $Line -le $TotalLines ] &&  read LINE
do
we=`awk '{print $2 $3 $4 $5}' | grep '[0-9]' | awk '{sum+=$1} END {print sum}'`
Line=$((Line+1))
echo $we
done < marks.txt

But i get the output as
Code:
# ./marks.sh
+ + wc -l
+ cat marks.txt
TotalLines=       6
+ Line=4
+ 0< marks.txt
+ [ 4 -le 6 ]
+ read LINE
+ + awk {print $2 $3 $4 $5}
+ grep [0-9]
+ awk {sum+=$1} END {print sum}
we=70750157
+ Line=5
+ echo 70750157
70750157
+ [ 5 -le 6 ]
+ read LINE

I think some problem with
Code:
awk '{sum+=$1} END {print sum}'

I think am going wrong somewhere..
Smilie
# 4  
Old 04-25-2013
Thanks for coming back on. Yes, the problem in your code seems to be on that long line with the two awk statements. Instead of trying to fix that logic, try this approach and see if it does what you want:
Code:
$ cat marks.txt
        MARKS LIST
               2013
Name english french chinese latin total_marks
wer        34    45     67      23
wqa       12      39     10      56
wsy       23      90     23      78

Code:
$ awk 'NF == 5 {sum = $2 + $3 + $4 + $5; print $0, sum } NF != 5 {print}' marks.txt
        MARKS LIST
               2013
Name english french chinese latin total_marks
wer        34    45     67      23 169
wqa       12      39     10      56 117
wsy       23      90     23      78 214

This User Gave Thanks to hanson44 For This Post:
# 5  
Old 04-25-2013
Calculate total value from a row

Thanks Hanson..

Yes i wanted as what You suggested me..

But please let me the logic how it worked..
# 6  
Old 04-25-2013
I would be happy to provide the logic of the awk script:
Code:
NF == 5 # if five fields on the line (wer, wqa, and wsy lines)
  {
  sum = $2 + $3 + $4 + $5; # get sum by adding fields 2 through 5
  print $0, sum # print entire input line followed by the sum
  } 
NF != 5 # if not five fields on the line (other lines)
  {
  print # just print the entire line
  }

# 7  
Old 04-25-2013
This may be shorten some
Code:
awk 'NF == 5 {sum = $2 + $3 + $4 + $5; print $0, sum; next } 1' marks.txt

Some more
Code:
awk 'NF == 5 {print $0, $2 + $3 + $4 + $5; next } 1' marks.txt


Last edited by Jotne; 04-25-2013 at 07:08 AM..
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