How could I sum, for a given number of rows each time, the the values for all the columns? Say, summing the column values for 2 rows each time. Ending up with a matrix like:
300 8 8 8 8 8
900 9 9 9 9 9
1000 7 7 7 7 7
I'm writing some perl code for the task but I'm courious if the task can be done 'easily' using awk. So far I've just got a flavour on how to sum the all the values for all columns{1} or how to sum all values for a row{2},
i.e {2}
awk '
BEGIN {FS=OFS="\t"}
{
sum=0;
for(i=1;i<=NF;i++)
{sum+=$i;}
print sum
}' matrix.txt
Once again I'm impressed how powerful awk is. Don't understand very well your one-liners but definitely will give myself a proper course and keep practicing,
One-liners are to impress people the power of UNIX.
Let me try to break them down into multi-line with comments so that you can appreciate the real power
awk '
# for every odd record number (NR) [NR%2 will equate to 1 (true) for odd NR]
NR%2 {
# for every field in each record (NF - number of fields)
for (i=1; i<=NF; i++) {
# sum them up and store in an associative array 'a', the index i corresponds to the column number
a+=$i
}
}
# for every even record number (NR) [!(NR%2) will equate to 1 (true) for even NR. ! means 'not']
!(NR%2) {
# for every field in each record (NF - number of fields)
for (i=1; i<=NF; i++) {
# add the associative array to the original column position
$i+=a
# reset assocaite array to zero
a=0
}
# print the whole row after the changes
print
}
' file