terminal - Bash command to calculate average on each row and each column -


suppose have log file marks.log , content looks this:

fname   lname   net algo     jack    miller  15  20   john    compton 12  20   susan   wilson  13  19   

i want add new column contains average each person, , new row contains average each course. result has this:

fname   lname   net  algo  avg jack    miller  15   20    17.5 john    compton 12   20    16 susan   wilson  13   19    16 average         13.3 19.6  - 

if data in datafile.txt, syntax awk like:

awk '   {   # if first row   if (nr==1)   print $0, "avg";   else   # print fields, average of fields 3 & 4   print $0,($3+$4)/2;   # total field 3 , field 4   t3+=$3; t4+=$4   }   # once done...   end {   # print final line   printf "overall average %.1f %.1f -\n",   # average of field 3 (nr number of records)   t3/(nr-1),   # average of field 4 (nr number of records)   t4/(nr-1);   }' datafile.txt 

that's long version comments. one-liner looks like:

awk '{if (nr==1) print $0, "avg"; else print $0,($3+$4)/2; t3+=$3; t4+=$4}end{printf "overall average %.1f %.1f -\n",t3/(nr-1),t4/(nr-1);}' datafile.txt 

this should match desired output.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -