How to trim the result of speedtest-cli to only output the download speed?
That's a job for awk
:
speedtest-cli --simple | awk 'NR==2{print$2}' # just the numeral
speedtest-cli --simple | awk 'NR==2{print$2" "$3}' # numeral and unit
Explanations
NR==2
– take line2
{print$2}
– print the second column (space-separated by default){print$2" "$3}
– print the second column followed by a space and the third one
With sed
it's a little more complicated:
speedtest-cli --simple | sed '/D/!d;s/.* \(.*\) .*/\1/' # just the numeral
speedtest-cli --simple | sed '/D/!d;s/[^ ]* \(.*\)/\1/' # numeral and unit
Explanations
/D/!d
– search for lines containingD
and don't (!
)d
elete them, but every other lines/A/B/
–s
ubstituteA
withB
.*
– take everything[^ ]*
– take everything that's not (^
) a space␣
(space character) – a literal space\(…\)
- take everything inside and save it as a group\1
– get the content of group 1
As speedtest-cli
is a python program and library it's fairly easy to make a minimal alternative program that only performs a download test and prints the output.
Open an editor, save as dl-speedtest.py
import speedtest
s = speedtest.Speedtest()
s.get_config()
s.get_best_server()
speed_bps = s.download()
speed_mbps = round(speed_bps / 1000 / 1000, 1)
print(speed_mbps)
run with python dl-speedtest.py
This gives the result in bps, as a floating point number Mbps rounded to one decimal as requested
The minimal version of speedtest-cli for this to work is 1.0.0 I think, you may need to use pip install speedtest-cli --upgrade
to upgrade.