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"