0

So I'm pretty new to Linux and I really can't figure it how to.

So I wanted to make a useful output for our monitoring Tool with speedtest-cli. We have to monitor download and upload speeds for multiple locations.

I made following script that breaks the output with awk and gives me the desired number (in this case only the number itself without text in front and behind the number)

SP=$(speedtest-cli 2>&1)
if [ $? -eq 0 ]
then
Down=$(echo $SP | gawk '{split($0,a,":"); print a[3]}' | \
                  gawk '{split($0,a," "); print a[1]}')
fi 
echo "$Down"

This script works as I want it to. But, I really would like a solution to return only the digits. So is it possible to search for the line "Download: 90.00 Mbit/s" and take the 90.00 and give that to output?



EDIT:

for anyone interested the script I wrote down there. it outputs <WAN_IP>,<Download>,<Upload> and if there is no connection it outputs 0.0.0.0,0,0

#!/bin/sh

SP=$(speedtest-cli 2>&1)

if [ $? -eq 0 ]
then

        From=$(echo "$SP" | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")

        Down=$(echo "$SP" | gawk '{if (match($0,/Download: ([[:digit:]]+\.[[:digit:]]+) Mbit\/s/,a)>0) print a[1]}')

        Up=$(echo "$SP" | gawk '{if (match ($0,/Upload: ([[:digit:]]+\.[[:digit:]]+) Mbit\/s/,a)>0) print a[1]}')

else
        From="0.0.0.0"

        Down="0"

        Up="0"

fi

echo "$From,$Down,$Up"
  • echo "Download: 90.00 Mbit/s" | awk '{print $2}'

    So you can add this as last iteration.

    – realpclaudio Feb 13 '20 at 13:20
  • You can do it using regex. Just match the digits – annahri Feb 13 '20 at 13:20
  • 1
    Welcome to the site. If possible, please add some example output of your speedtest call so it is easier to help you design a suitable regular expression. – AdminBee Feb 13 '20 at 13:30
  • Thank you @AdminBee . If the Speedtest gives me the Output "Download: 90.00 Mbit/s" I want to print just "90.00". I got it working with grep with a tmp file, but I would like to do that without a file. So when I tried it with the variable, grep did no longer work properly.. – Marinus Pollinger Feb 13 '20 at 13:42
  • Thank you. The reason why I was asking for the example output is the formatting of the output. Does the relevant line start with the word Download immediately, or is there some indentation with leading whitespace? Does the line end immediately after the Mbit/s, or are there trailing characters? Are there further lines in the output that might start with Download or contain similar patterns? I think your problem can be solved, but that kind of information would be necessary to provide good advice. – AdminBee Feb 13 '20 at 13:50
  • @AdminBee Okay! I try to deliver this immediatly the next time :) So part of my Problem is, that the output in the variable doesn't really come in lines.. If I echo $SP it is just a long String. When I store the output Download: 90.00 Mbit/s is one line. In the variable it just in the middle of the long string with every output line – Marinus Pollinger Feb 13 '20 at 13:55
  • I see. The problem of unpreserved line structure can be avoided by quoting the variable as in "$SP"(which you should do anyway in most cases). – AdminBee Feb 13 '20 at 14:05

2 Answers2

1

Since you explicitly use gawk, you can use the match() function to look for the pattern "Download: number Mbit/s" and extract the actual value of that pattern found in your string as follows:

gawk '{if (match($0,/Download: ([[:digit:]]+\.[[:digit:]]+) Mbit\/s/,a)>0) print a[1]}'

This will

  • determine if the pattern was found in the first place, and
  • put all ( ... )-enclosed sub-groups of the RegExp in the elements of the array a

from where you can then simply use entry 1 (since there is only one such sub-group in the RegExp) .

AdminBee
  • 22,803
  • AWESOME! That was the solution. Thank you so much!! and Thanks for the tip with quotation of variables! I didn't know that, it's way better then using it without the quotes! – Marinus Pollinger Feb 13 '20 at 14:13
1

speedtest-cli has a --simple switch which makes the output simple (try speedtest-cli --help for more information):

# speedtest-cli --simple --no-upload
Ping: 18.833 ms
Download: 33.88 Mbit/s
Upload: 0.00 Mbit/s

So we can get the download-only speed as a number thus:

# speedtest-cli --simple --no-upload|grep "^Download:"|cut -d" " -f2
34.92

This uses --no-upload to save a bit of time by not testing upload speed, grep to extract just the download line, and cut to extract the second field i.e. the number in Mbit/s.

gogoud
  • 2,672
  • Thank you for your answer! it works like that for Download and Upload speed. We want to monitor the IP Adress aswell that's why I didn't use the --simple. But that is my fault for not mentioning! – Marinus Pollinger Feb 13 '20 at 14:15