Although I am not at all sure how and in which sequence you have shown the sum of fields for each line in Input_file at very bottom of your shown expected output. My above code will give SUM of fields in sequence of lines from 2nd line to till last line, if above doesn't meet your requirements then please get back to us with complete requirement details and expected sample output(with details).
[akshay@localhost tmp]$ cat test.awk
BEGIN{
# if variable cols not defined exit with below message to user
if(cols==""){ print "Please mention column number separated by comma" ; exit }
# split string cols separated by comma and save columns to be added in array c
split(cols,c,/,/)
}
FNR>1{
# Skip header and loop through array c and sum up fields, save result in array s
for(i in c)
s[c]+= $(c)
}1
END{
# Loop through fields and print result
for(i=1; i<=NF; i++){
printf("%s%s",(i==1?"":OFS),(i in s ? s : "NA"))
}
print ""
}
The following produces the output you said you want. (I don't understand why you want two digits after the decimal in the sum of field 2 since all of the input lines only have one digit after the decimal point in field 2; but the output matches what you requested.)
awk '
BEGIN { FS = OFS = "|"
}
NR > 1 {for(i = 2; i <= 5; i++)
s += $i
}
1
END { printf("%s%.2f%s%.2f%s%.1f%s%.2f\n", OFS, s[2], OFS, s[3], OFS, s[4],
OFS, s[5])
}' file
Thanks all. The first solution probably was for row sum. Second and Third perfectly worked. Third solution is more crisp and clear, I liked it the most. Thanks a ton guys.