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
182 views
in Technique[技术] by (71.8m points)

linux - How do I calculate the mean of a column

Anyone know how can I calculate the mean of one these columns (on linux)??

sda               2.91    20.44    6.13    2.95   217.53   186.67    44.55     0.84   92.97
sda               0.00     0.00    2.00    0.00    80.00     0.00    40.00     0.22  110.00 
sda               0.00     0.00    2.00    0.00   144.00     0.00    72.00     0.71  100.00 
sda               0.00    64.00    0.00    1.00     0.00     8.00     8.00     2.63   10.00
sda               0.00     1.84    0.31    1.38    22.09   104.29    74.91     3.39 2291.82 
sda               0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00  

For example: mean(column 2)

question from:https://stackoverflow.com/questions/3122442/how-do-i-calculate-the-mean-of-a-column

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

1 Reply

0 votes
by (71.8m points)

Awk:

awk '{ total += $2 } END { print total/NR }' yourFile.whatever

Read as:

  • For each line, add column 2 to a variable 'total'.
  • At the end of the file, print 'total' divided by the number of records.

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

...