Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic awk script to calculate average on any field through command line argument

I want to write a generic awk script that can take as input a file and a field number (in that file) and give me the average value of that field in that file. I would use it something like this:

bash$ avg.awk 3 input.file
22
bash$ avg.awk 4 input.file
2001

Of course, I can write the script if I know which field (e.g., $3) I am going to average beforehand. That would be something like this:

//{tot+=$3; count++}
END{
    print tot/count;
}

But I want to be able to change the field I want to average through a command line option. Is that possible? Thanks!

like image 672
Ritesh Avatar asked Sep 16 '09 16:09

Ritesh


People also ask

What is awk'- f option?

For example: awk -F, ' program ' input-files. sets FS to the ' , ' character. Notice that the option uses an uppercase ' F ' instead of a lowercase ' f '. The latter option ( -f ) specifies a file containing an awk program.

What does 1 mean in awk?

1 means to print every line. The awk statement is same as writing: awk -F"=" '{OFS="=";gsub(",",";",$2);print $0;}' Copy link CC BY-SA 3.0.


1 Answers

This one will do what you want:

$ cat avg.awk
#!/usr/bin/env awk -f
# Calculate average, syntax: avg.awk field-number file
BEGIN { field = ARGV[1]; ARGV[1] = "" }
{ sum += $field }
END { print sum / NR }

$ cat data
1 5 7
3 6 5
8 4 6

$ avg.awk 1 data
4

$ avg.awk 2 data
5

$ avg.awk 3 data
6
like image 66
Hai Vu Avatar answered Sep 30 '22 18:09

Hai Vu