How to trim the result of speedtest-cli to only output the download speed?

I'd like to periodically display my speedtest download speed result in indicator-sysmonitor.

speedtest-cli has a somewhat trimmed output if you run

$ speedtest-cli --simple
Ping: 50.808 ms
Download: 10.87 Mbit/s
Upload: 4.47 Mbit/s 

Is there any way to trim the output even more, down to just the download speed numeral?


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 line 2
  • {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 containing D and don't (!) delete them, but every other line
  • s/A/B/substitute A with B
  • .* – 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.


This will also work:

speedtest-cli --simple | grep -E "Download:\s*" | sed -r 's/Download:\s*//'

You can try this:

speedtest-cli --simple | grep "Download: " | sed "s/Download: //g"

And then there's:

speedtest-cli --simple | grep Download | awk '{print $2}'

Like dessert's first option though without the line selector.