-1

I need to setup a one line command to get an average of huge amount of numbers in one column in a file contains multiple columns, we need to add the arguments for the file name and the number of column needed from the big file

Example: # avgerage_column 3 test

average_column should be the script
3 is the number of the column in the big file
test is the big file


Sample:

210  56687  1792 18 55 26  1  
188  21949   961  9 57 34  0  
201   1257   521  1 46 54  0  
260  11455   641  4 47 49  0  
244  16466   756  7 38 55  0  
 54   1570   651  1 14 85  0  
161    619   583  0 28 72  0  
 88    479   600  0 26 74  0  
 30    394   670  0 37 63  0  
  6   3713   369  3 79 18  0  
 24  16109   553 16 60 24  0  
  7    877   482  0 68 32  0  
 84    845   525  0 63 36  0  
 31    844   492  0 79 21  0  

Thanks

mshafey
  • 89
  • 1
  • 9

1 Answers1

0

In AWK:

awk  'BEGIN { total = 0; count = 0 } { total += $3; count += 1; } END { avg = total / count; print avg} ' input.txt
  • $3 is the third field
  • input.txt is your big file.

Output:

685.429
Jacek Trociński
  • 882
  • 1
  • 8
  • 23
  • then i can add that line in a script after editing $3 with $$1 as $1 is my first argument .. also to checnge input.txt with $2 .... #the needed command is # your_code 3 input.txt – mshafey Nov 28 '16 at 15:27
  • @mshafey sure you can make a bash script, assign the parameters to some variables and use them as a part of this AWK script, I'll leave that up to you. – Jacek Trociński Nov 28 '16 at 15:34