MacOS – Bash command to calculate average on each row and each column

bashmacosterminal

Suppose we have a log file like marks.log and the content looks something like this:

Fname   Lname   Net Algo    
Jack    Miller  15  20  
John    Compton 12  20  
Susan   Wilson  13  19  

I want to add a new column that contains average for each person (row), and a new row that contains average for each course (column). I'm new to bash can i can't figure the syntax for loops and awk etc.

Best Answer

Based on what you did so far:

awk '{count++; col2 += $2; col3 += $3; print ($2+$3)/2}
     {END print col2/count, col3/count}' marks.log