Reputation: 134
I have a file named bt.B.1.log that looks like this:
.
.
.
Time in seconds = 260.37
.
.
.
Time in seconds = 260.04
.
.
.
and so on for 40 records of Time in seconds (dots represent useless lines). I'm supposed to extract those times from the file and calculate/print the average to a .dat file (t1avg.dat) without using intermediate files. I've been able to do so using intermediate files:
awk '/Time in seconds/ {print $5}' bt.B.1.log > t1.dat
awk '{sum+=$0} END {print sum/NR}' t1.dat > t1avg.dat
So the approach is to somehow pass the regular expression that appears in the first awk command as a parameter in the second command, telling awk to calculate the sum over the resulting lines of the regular expression, all reading from a single file (bt.B.1.log) and writing to t1avg.dat (suppressing the creation of t1.dat). So far I've reached the following code, which is giving the wrong output:
awk '{sum+=/Time in seconds/$5; print sum/NR}' bt.B.1.log
Any ideas?
Upvotes: 4
Views: 338
Reputation: 103824
You can split on =
as well:
$ echo "$e"
.
.
.
Time in seconds = 260.37
.
.
.
Time in seconds = 260.04
.
$ echo "$e" | awk -F= '/Time in seconds/ {s+=$2; c++} END{print s/c}'
260.205
Upvotes: 1
Reputation: 113834
awk '/Time in seconds/ {s+=$5;c++} END{print s/c}' bt.B.1.log >t1avg.dat
s
is the sum of the times and c
is the count. Thus, s/c
is the average.
Upvotes: 2