-2

So I have a shell script that is writing to a specific file. Within that file, there exists some lines in a format similar to

Time taken: 123.45 ms

I would like to find every instance of this specific line and find the mean of all of the times in this file.

Edit: So I am sometimes dynamically appending to a file, lets say sample.txt. The problem that I am having sometimes is that awk sometimes misses the last 1 or 2 instances of this format. I think that awk is trying to access the file and read it before the file is done writing and there is a data race somewhere. How can I avoid this in the future?

manatee
  • 15
  • 4
  • `but I don't really know the specifics` [google how to use grep](https://www.google.com/search?q=how+to+use+grep) – KamilCuk Feb 05 '20 at 01:30
  • [Bash only supports integer arithmetic.](https://mywiki.wooledge.org/BashFAQ/022) You want another language, like Python, Perl, or AWK, in descending order of readability. – l0b0 Feb 05 '20 at 02:11
  • Adding to the comment by I0b0: If you want to do it with shell scripting, you can use Zsh. It supports float arithmetic, and the syntax is fairly similar to bash, so that you will get familiar with it soon. It is also a good interactive shell, so if you like it, you can replace bash. – user1934428 Feb 05 '20 at 08:48

1 Answers1

0

There are quite a few options:

awk '/Time taken: / { S+=$3; N++} END { printf ("%0.3f\n", S/N)}' sample.txt
  • /Time taken: / - only process lines that match
  • S+=$3 - sum the numbers in field 3 (if your line is not only 'Time taken: ##.### ms', you will need to adjust the field number appropriately
  • N++ - number matching lines

Similar for Perl:

perl -ae '$S+=$F[2],$N++ if /Time taken: /; END { print sprintf("%0.3f\n",$S/$N)}' sample.txt

Two steps:

grep 'Time taken' sample.txt | awk '{ S+=$3} END { printf ("%0.3f\n", S/NR)}'

Taking advantage of awk's line number NR

Sorin
  • 5,201
  • 2
  • 18
  • 45