简体   繁体   中英

Using awk to calculate and print the average of values obtained via regular expression

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM