I'd do this pretty much the same way. Only difference, I'd 'exit' after the last row to be summed was reached (for performance, to avoid reading the rest of the file).
I'd also create the awk script as an executable, to remove the need for the shell to delegate to awk (i.e. include the header
#!/usr/bin/awk -f)
Code:
#!/usr/bin/awk -f
BEGIN {
if (! startRow || ! endRow)
{
exitMessage="You must supply startRow and endRow values."; exit
}
}
NR>endRow {exit}
NR>=startrow {
for (i=1;i<=NF;i++)
{
totals[i]+=$i
}
}
END {
for (j=1;j<=i;j++)
{
printf "%s ",totals[j]
}
printf exitMessage"\n"
}
You would need to give execution permissions on the script...
chmod 755 sumcols
And you would call it using...
sumcols -v startRow=1 -v endRow=3 yourInputFile