Reputation: 193
I have a file that has say 50 columns. I want to sum columns 10 to 50, since columns 1-9 have text data. I want to include every column in the output and the final column is the sum.
I can do this using a very long statement:
awk '{X=$0}{split(X,x)}{print X , x[10]+x[11]+x[12]+x[13]+x[14]+x[15]+x[16]+x[17]+x[18]+x[19]+x[20]+x[21]+x[22]+x[23]+x[24]+x[25]+x[26]+x[27]+x[28]+x[29]+x[30]+x[31]+x[32]+x[33]+x[34]+x[35]+x[36]+x[37]+x[38]+x[39]+x[40]+x[41]+x[42]+x[43]+x[44]+x[45]+x[46]+x[47]+x[48]+x[49]+x[50]}' input > output
I was wondering how I could go about cleaning this up? This works fine, but is very slow on my large files.
(also, I have another awk oneliner I'm running after this to drop all columns where the sum is < 1000 using awk '($51 > 999 )'
, can I combine this into the above?)
Upvotes: 0
Views: 501
Reputation: 67507
@RavinderSingh13's script can be simplified to
$ awk '{NF++; for(i=10;i<NF;i++) $NF+=$i} $NF>999' file
Upvotes: 1
Reputation: 133538
Could you please try following, based on your shown attempts only, since no samples are given so its not tested(this includes both of OP's shown attempts), this also considers that your field are space delimited.
awk '
FNR==1{
print
next
}
{
sum=0
for(i=10;i<=NF;i++){ sum+=$i }
if(sum>999) { print $0,sum }
}' Input_file
Upvotes: 3