Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

AWK - sum row, columns x to y and print as new column

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?)

question from:https://stackoverflow.com/questions/66051826/awk-sum-row-columns-x-to-y-and-print-as-new-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...