我需要用wget下载几个文件,并测量下载速度。
例如我用以下命令下载
wget -O /dev/null http://ftp.bit.nl/pub/OpenBSD/4.7/i386/floppy47.fs http://ftp.bit.nl/pub/OpenBSD/4.7/i386/floppyB47.fs输出结果是
--2010-10-11 18:56:00-- http://ftp.bit.nl/pub/OpenBSD/4.7/i386/floppy47.fs
Resolving ftp.bit.nl... 213.136.12.213, 2001:7b8:3:37:20e:cff:fe4d:69ac
Connecting to ftp.bit.nl|213.136.12.213|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1474560 (1.4M) [text/plain]
Saving to: `/dev/null'
100%[==============================================================>] 1,474,560 481K/s in 3.0s
2010-10-11 18:56:03 (481 KB/s) - `/dev/null' saved [1474560/1474560]
--2010-10-11 18:56:03-- http://ftp.bit.nl/pub/OpenBSD/4.7/i386/floppyB47.fs
Reusing existing connection to ftp.bit.nl:80.
HTTP request sent, awaiting response... 200 OK
Length: 1474560 (1.4M) [text/plain]
Saving to: `/dev/null'
100%[==============================================================>] 1,474,560 499K/s in 2.9s
2010-10-11 18:56:06 (499 KB/s) - `/dev/null' saved [1474560/1474560]
FINISHED --2010-10-11 18:56:06--
Downloaded: 2 files, 2.8M in 5.9s (490 KB/s)我需要grep总的下载速度,也就是字符串490 KB/s。我该怎么做呢?
P.S.可能需要考虑这样的情况,即我们实际上只下载一个文件,因此不会有从FINISHED开始的最终输出
发布于 2010-10-12 03:05:34
Update,使用sed的grep风格的版本:
wget ... 2>&1 | sed -n '$,$s/.*(\(.*\)).*/\1/p'旧版本:
我想,将文件大小除以下载后的下载时间会更容易。;-)
(/usr/bin/time -p wget ... 2>&1 >/dev/null; ls -l newfile) | \
awk '
NR==1 {t=$2};
NR==4 {printf("rate=%f bytes/second\n", $5/t)}
'第一行awk以可变的t格式存储"real xx.xx“的运行时间。第二行awk将文件大小(ls -l的第5列)除以时间,并将其作为速率输出。
发布于 2010-10-12 03:16:07
这对我很有效,使用你的wget -O /dev/null <resource>
我使用的正则表达式是\([0-9.]\+ [KM]B/s\)
但请注意,我必须将stderr重定向到stdout上,所以命令是:
wget -O /dev/null http://example.com/index.html 2>&1 | grep '\([0-9.]\+ [KM]B/s\)'这允许像923 KB/s和1.4 MB/s这样的东西
grep只查找匹配项。要获取值,您可以改用sed:
wget -O /dev/null http://example.com/index.html 2>&1 |
sed -e 's|^.*(\([0-9.]\+ [KM]B/s\)).*$|\1|'发布于 2014-12-07 23:09:27
当只有一个文件被下载时,这是有效的。
我开始使用sed来获得wget的速度,但我发现它很烦人,所以我转而使用grep。
这是我的命令:
wget ... 2>&1 | grep -o "[0-9.]\+ [KM]*B/s"-o选项表示它只返回该部分。它匹配10位数字中的1位或更多位,然后是一个空格。然后可在B/s之前选择K或M
这将返回423 KB/s (例如)。
要仅对单元使用grep,请使用grep -o "[KM]*B/s";对于数字,请使用grep -o "[0123456789]\+。
https://stackoverflow.com/questions/3909128
复制相似问题