Using awk to calculate and print the average value obtained using a regular expression

I have a file called bt.B.1.log that looks like this:

.
.
.
Time in seconds =                   260.37
.
.
.
Time in seconds =                   260.04
.
.
.

etc. for 40 time records in seconds (dots represent useless strings). I have to extract this data from the file and calculate / print the average value of the .dat file (t1avg.dat) without using intermediate files. I was able to do this with 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

, , - , awk , awk , ( bt.B.1.log) t1avg.dat( t1.dat). , :

awk '{sum+=/Time in seconds/$5; print sum/NR}' bt.B.1.log

?

+4
2
awk '/Time in seconds/ {s+=$5;c++} END{print s/c}' bt.B.1.log >t1avg.dat

s - , c - . , s/c .

+2

=:

$ 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
+1

Source: https://habr.com/ru/post/1584959/


All Articles