Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

smart program!! 1

Status
Not open for further replies.

tonivm

Technical User
Mar 2, 2006
64
ES
Hi everybody:
I have awk script which calcule the total sum of each column like this:
Code:
awk 'BEGIN{OFS=" };{s2+=$2;s3+=$3;s4+=$4;s5+=$5;s6+=$6;s7+=$7;s8+=$8;s9+=$9;
s10+=$10;s11+=$11;s12+=$12}; END{print s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12}' file1 > file2.
so I would like reduce the line with a simple line, for example this sum into a for stament.
Any suggestion.
Thanks in advance. :D
 
Use an array:
Code:
{for(i=1;i<=NF;++i)s[i]+=$i}

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Code:
awk '
{
  for(i=1; i<=NF; i++)
     arr[i] += $i
  nf=NF
}
END {
  for(i=1; i<=nf; i++)
    printf("%d%s", arr[i], (i==nf) ? ORS : OFS)
}' file1 > file2.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Code:
{ for (i=2; i<=NF; i++)
    s[i] += $i
}
END {
  for (i=2; i in s; i++)
  { printf "%s%f", x, s[i]
    x = " "
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top