Issue
Here is the output given by speedtest-cli: (personal information redacted with test data)
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from ------ (xxx.xxx.xxx.xxx)...
Selecting best server based on latency...
Hosted by ------- (------, --) [15.00 km]: 50.00 ms
Testing download speed........................................
Download: 60.00 Mbit/s
Testing upload speed..................................................
Upload: 10.00 Mbit/s
Where I want the output to be a comma separated line of ping, dl, ul:
50.00, 60.00, 10.00
I have been working on a solution and have come up with this:
speedtest-cli | sed -n "5p;7p;9p" | grep -oE "[[:digit:]]{1,}" | tr '\n' ,
Which outputs:
15,00,50,00,60,00,10,00,
Which is close to what I want. Except that it is including the distance (15.00km) from the 5th line and splitting based on . as well. Is there a better way to do this using awk or something similar?
Solution
Using awk you can do:
speedtest-cli | awk -v ORS=', ' '/^(Hosted|Download|Upload)/{print $(NF-1)}'
50.00, 60.00, 10.00,
To use newline instead of trailing ,
use:
speedtest-cli | awk -v ORS=', ' '/^(Hosted |Download:)/{print $(NF-1)}
/^Upload:/{printf "%s%s", $(NF-1), RS}'
50.00, 60.00, 10.00
Answered By - anubhava