This is a self-answered question. I've already written an answer but, if you have a better one, please post it.
I'm developing a program and it's printing some time results:
$ homa.py
Creating instance rd = NetworkInfo(): 0.0071680546
Creating instance rd = NetworkInfo(): 0.0083100796
Creating instance rd = NetworkInfo(): 0.0121581554
Creating instance rd = NetworkInfo(): 0.0205171108
Creating instance rd = NetworkInfo(): 0.0067570210
Creating instance rd = NetworkInfo(): 0.0215399265
Creating instance rd = NetworkInfo(): 0.0170729160
Creating instance rd = NetworkInfo(): 0.0088560581
Creating instance rd = NetworkInfo(): 0.0079369545
Creating instance rd = NetworkInfo(): 0.0091099739
Creating instance rd = NetworkInfo(): 0.0085971355
Creating instance rd = NetworkInfo(): 0.0137989521
Creating instance rd = NetworkInfo(): 0.0081419945
Creating instance rd = NetworkInfo(): 0.0090279579
Creating instance rd = NetworkInfo(): 0.0070800781
Creating instance rd = NetworkInfo(): 0.0079300404
^C
homa.py refresh() closed by SIGTERM
It's too difficult to glance at the list and get an average time.
How can I quickly get the average time?
Use awk:
Or paste the results into awk.
Although, since you already are running a python script, it would be better to just add code to the python script to calculate a running average and print it with each value.
1. Copy the column of numbers using rectangle
Hold down the Ctrl key and Left mouse button and drag the mouse to highlight the column of numbers:
NOTE: As soon as you highlight the rectangle it is copied to the clipboard.
2. Run the program
n
Then type
n
+ Enter.Use the middle-mouse button to paste from the clipboard. Then press Enter to tally the results.
3. Install
n
on your systemThe
n
program is a simple python script:Copy the above python script to a file named
n
. Then
file should be in your path. If not in your path you must call it with the directory prefix. For example you would have to type/home/me/stuff/n
instead of justn
.You need to flag the
n
program as executable. For examplechmod a+x /home/me/stuff/n
.4. Summary
n
will take numbers copied not just fromgnome-terminal
but also from Libre Calc, pyCharm and many other programs that can copy to the system clipboard.Having to add up or calculate the average for a set of numbers has been a PITA for a long time for me. So I whipped up this little script in just a few minutes. Writing this Q & A actually took longer :)
n
should run on any Python version from2.7.12
to3.14
or in other words Ubuntu 16.04 through Ubuntu 24.10.Other Answers
There are other great answers specifically tailored to the question but are not robust. For example another program displays output of WiFi speed from Linux to Android 13 phone:
The
n
python program still works without any tweaking ofawk
print positions. Additionally, for myself anyway,awk
is frankly awkward to remember the syntax and arguments. Making a script and constantly revising it based on column position doesn't speed up the workflow.A variation on WinEunuuchs2Unix's answer;
Another way would be:
~/bin/crunch_numbers.sh
;~/bin/crunch_numbers.sh
:This way, the workflow would consist of just:
The output would be copy-pastable by selecting it / right-clicking it / clicking on "Copy", if needed.
An advantage to this method is that it doesn't require a terminal, which is useful when copying stuff from somewhere else (say Calc).
A disadvantage to this method is that I'm pretty sure WinEunuuchs2Unix's script is more accurate than my script. If floating-point precision is a concern, one should probably combine this method with their script instead.
An alternative using
cut
to select the sixth column andjq
to calculate the average.The one-liner below allows you to run the script, see the results as they come in, and calculate the average at the end.
python -u
is to get the output unbuffered when sending it to the pipe, so you can still see the output onstdout
while it comes in|tee >(...)
is to duplicate the output betweenstdout
and the further processing in the pipetrap '' INT
is to run the pipe when you press Ctrl-C (SIGINT) instead of also killing the pipe before it startscut -d" " -f 6
is to split the lines into columns with space as delimiter and showing only the 6th columnjq -s "add / length"
adds all lines and divides by the number of linesPython script used for testing:
Example run:
Of course you can also copy the output with the mouse and use a simpler command to paste the clipboards content into the pipe: