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
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
}
`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..
"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: