8

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?

1
  • 1
    You can use --no-upload option to don't display upload either. Oct 4, 2017 at 8:36

5 Answers 5

12

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.

2
  • Beauty, thanks! As great as @dessert's answer is, this is what I ended up using due to efficiency. I'm polling it every few minutes so trimming the fat on unnecessary calls is beneficial to me.
    – Adam
    Oct 4, 2017 at 12:50
  • @Adam You should edit your question and change it accordingly then – the title doesn't match this answer currently.
    – dessert
    Oct 4, 2017 at 12:57
12

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
1

This will also work:

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

You can try this:

speedtest-cli --simple | grep "Download: " | sed "s/Download: //g"
1
  • 2
    Instead of grep combined with sed, you can simply use sed -n and append p at the end of the expression, like speedtest-cli --simple | sed -nr 's/Download:\s*//p'.
    – Ruslan
    Oct 4, 2017 at 10:51
1

And then there's:

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

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

1
  • 1
    It's rarely useful to do grep ... | awk - speedtest-cli --simple | awk '/Download/{print $2}'
    – muru
    Oct 4, 2017 at 11:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .