Calculate total value from a row

HI

I have a file

# 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..:mad:

Thanks in advance

Any chance that's a homework problem?

Hi

This is not a homework problem :cool:

I proceeded the following 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

# ./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

awk '{sum+=$1} END {print sum}'

I think am going wrong somewhere..
:wall:

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:

$ 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
$ 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
1 Like

Thanks Hanson..

Yes i wanted as what You suggested me..

But please let me the logic how it worked..

I would be happy to provide the logic of the awk script:

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
  }

This may be shorten some

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

Some more

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

Hi Hanson

`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 anywhere:D

$ 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:

$ 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:

$ awk '{print $2 + $3 + $4 + $5}' marks.txt
0
0
0
169
117
214

Hi

Thank You..
But why there are zeros..
If those zeros are truncated, then my requirement will be totally met..

# awk '{print $2+$3+$4+$5}' marks.txt
0
0
0
169
117
214

The marks.txt file contents:

# 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

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:

$ awk 'NF == 5 {print $2+$3+$4+$5}' marks.txt
169
117
214
1 Like

Thanks.. Its finally worked fine..