Ada*_*dam 8 command-line text-processing
我想定期在指标系统监视器中显示我的速度测试下载速度结果。
如果您运行,speedtest-cli 的输出会有所调整
$ speedtest-cli --simple
Ping: 50.808 ms
Download: 10.87 Mbit/s
Upload: 4.47 Mbit/s
Run Code Online (Sandbox Code Playgroud)
有没有什么办法可以进一步修剪输出,直到下载速度数字?
des*_*ert 12
这是一份工作awk:
speedtest-cli --simple | awk 'NR==2{print$2}' # just the numeral
speedtest-cli --simple | awk 'NR==2{print$2" "$3}' # numeral and unit
Run Code Online (Sandbox Code Playgroud)
NR==2 – 走线 2{print$2} – 打印第二列(默认以空格分隔){print$2" "$3} – 打印第二列,后跟一个空格和第三列随着sed这是一个有点复杂:
speedtest-cli --simple | sed '/D/!d;s/.* \(.*\) .*/\1/' # just the numeral
speedtest-cli --simple | sed '/D/!d;s/[^ ]* \(.*\)/\1/' # numeral and unit
Run Code Online (Sandbox Code Playgroud)
/D/!d– 搜索包含D和不 ( !)d删除它们的行,但每隔一行s/A/B/- substituteA与B.* – 拿走一切[^ ]*– 把所有不是 ( ^) 空格的东西都拿走? (空格字符) – 文字空间\(…\) - 把里面的所有东西都保存为一个组\1 – 获取第 1 组的内容Pel*_*lle 12
作为speedtest-cli一个python程序和库,制作一个只执行下载测试并打印输出的最小替代程序相当容易。
打开编辑器,另存为 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 Code Online (Sandbox Code Playgroud)
运行 python dl-speedtest.py
这给出了以bps为单位的结果,作为一个浮点数Mbps 根据要求四舍五入到一位小数
用于此工作的最低版本 speedtest-cli 我认为是 1.0.0,您可能需要使用pip install speedtest-cli --upgrade来升级。