How to sum multiple column output with awk ?

Hi Experts, I am trying to sum multiple columns and rows with awk ,

I want the sum of :
1] Horizontal Sum: (rows sum):
2] Vertical Sum: (Column's sum]

details:

# cat file1
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
40 31 32 33 34 35 36 37 38 39
70 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
20 81 82 83 84 85 86 87 88 89
05 91 92 93 94 95 96 97 98 99

I am trying with this :

Horizontal sum:

# awk '{X=$0}{split(X,x)}{print X , " =" x[1]+x[2]+x[3]+x[4]+x[5]+x[6]+x[7]+x[8]+x[9]+x[10] }' file1| tr " " "\t"
10      11      12      13      14      15      16      17      18      19              =145
20      21      22      23      24      25      26      27      28      29              =245
40      31      32      33      34      35      36      37      38      39              =355
70      41      42      43      44      45      46      47      48      49              =475
50      51      52      53      54      55      56      57      58      59              =545
60      61      62      63      64      65      66      67      68      69              =645
70      71      72      73      74      75      76      77      78      79              =745
20      81      82      83      84      85      86      87      88      89              =785
05      91      92      93      94      95      96      97      98      99              =860
 

Vertical Sum:

 $ awk '{s1+=$1;s2+=$2;s3+=$3;s4+=$4;s5+=$5;s6+=$6;s7+=$7;s8+=$8;s9+=$9;s10+=$10}END{print s1,s2,s3,s4,s5,s6,s7,s8,s9,s10}' file1 | tr " " "\t" 
345     459     468     477     486     495     504     513     522     531

I want the output to be look like this:

10      11      12      13      14      15      16      17      18      19              =145
20      21      22      23      24      25      26      27      28      29              =245
40      31      32      33      34      35      36      37      38      39              =355
70      41      42      43      44      45      46      47      48      49              =475
50      51      52      53      54      55      56      57      58      59              =545
60      61      62      63      64      65      66      67      68      69              =645
70      71      72      73      74      75      76      77      78      79              =745
20      81      82      83      84      85      86      87      88      89              =785
05      91      92      93      94      95      96      97      98      99              =860
345     459     468     477     486     495     504     513     522     531

How to make it easy and into one command, with loops and not by typing 1 to 10 iterations.

Thanks,..

You can just sum it up as you go through each line:

$ cat myScript
awk '{ 
  TR=0
  for( I = 1; I <= NF; I++ ) {
    TR += $I
    TC += $I
    printf( "%6d", $I )
  }
  print "  = " TR
  TF = NF
}

END {
  for( I = 1; I <= TF; I++ )
    printf "%6d", TC
  print ""
}
' file

$ cat file
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
40 31 32 33 34 35 36 37 38 39
70 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
20 81 82 83 84 85 86 87 88 89
05 91 92 93 94 95 96 97 98 99

$ ./myScript
    10    11    12    13    14    15    16    17    18    19  = 145
    20    21    22    23    24    25    26    27    28    29  = 245
    40    31    32    33    34    35    36    37    38    39  = 355
    70    41    42    43    44    45    46    47    48    49  = 475
    50    51    52    53    54    55    56    57    58    59  = 545
    60    61    62    63    64    65    66    67    68    69  = 645
    70    71    72    73    74    75    76    77    78    79  = 745
    20    81    82    83    84    85    86    87    88    89  = 785
     5    91    92    93    94    95    96    97    98    99  = 860
   345   459   468   477   486   495   504   513   522   531
1 Like

Thanks Scott,
This works like charm and great coding, still trying to understand it.
Thanks a lot.