Reputation: 4332
I have a file which contains lines of data like below
300,,,292,15.4,0,04/16/12 20:10:39
200,,,292,15.4,0,04/16/12 20:10:39
100,,,292,15.4,0,04/16/12 20:10:39
Using awk i am summing up the first column like this
awk -F ',' '{ x = x + $1 } END { print x }' MyFile
Which is retuning 600 no problems.
I want to modify this command to also calculate the amount of lines in the file and then instead of { print x }
i want to { print x / y }
where y is the number of lines in the file.
So it would return 600 / 3 = 200.
Upvotes: 1
Views: 6223
Reputation: 753695
The number of records processed so far is in NR:
awk -F, '{ x += $1 } END { print x " " x/NR }' MyFile
NR counts across all files processed; here, that's a single file. There's also FNR in at least some versions of awk
; that counts the number of records processed in the current file. In this example, NR == FNR at all times because there's a single file.
Upvotes: 4
Reputation: 23065
Did you try NR for the number of records?
awk -F ',' '{ x = x + $1 } END { print x/NR }' MyFile
Upvotes: 0