Linux 如何从wget输出grep下载速度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3909128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:38:50  来源:igfitidea点击:

How to grep download speed from wget output?

linuxcommand-linegrepwget

提问by Nickolai Leschov

I need to download several files with wgetand measure download speed.

我需要下载几个文件wget并测量下载速度。

e.g. I download with

例如我下载

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

and the output is

输出是

--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)

I need to grep the total download speed, that is, the string 490 KB/s. How do I do this?

我需要 grep 总下载速度,即 string 490 KB/s。我该怎么做呢?

P.S. May need to account for the case that we will actually download only one file, so there won't be final output starting with FINISHED

PS 可能需要考虑到我们实际上只会下载一个文件的情况,所以最终输出不会以 FINISHED

回答by Peter G.

Update, a grep-style version using sed:

更新,使用 sed 的 grep 样式版本:

wget ... 2>&1 | sed -n '$,$s/.*(\(.*\)).*//p'

Old version:

旧版本:

I thought, it's easier to divide the file size by the download time after the download. ;-)

我认为,下载后将文件大小除以下载时间更容易。;-)

(/usr/bin/time -p wget ... 2>&1 >/dev/null; ls -l newfile) | \
awk '
   NR==1 {t=};
   NR==4 {printf("rate=%f bytes/second\n", /t)}
'

The first awk line stores the elapsed real time of "real xx.xx" in variabe t. The second awk line divides the file size (column 5 of ls -l) by the time and outputs this as the rate.

第一个 awk 行在 variabe 中存储“real xx.xx”经过的实时时间t。第二行 awk 将文件大小( 的第 5 列ls -l)除以时间并将其作为速率输出。

回答by Stephen P

This worked for me, using your wget -O /dev/null <resource>

这对我有用,使用你的 wget -O /dev/null <resource>

The regex I used was \([0-9.]\+ [KM]B/s\)

我使用的正则表达式是 \([0-9.]\+ [KM]B/s\)

But note I had to redirect stderronto stdoutso the command was:

但是请注意,我不得不重新定向stderrstdout这样的命令是:

wget -O /dev/null http://example.com/index.html 2>&1 | grep '\([0-9.]\+ [KM]B/s\)'

This allows things like 923 KB/sand 1.4 MB/s

这允许像923 KB/s1.4 MB/s



grepjust finds matches. To get the value(s) you can use sedinstead:

grep只找到匹配项。要获取您可以使用的值sed

wget -O /dev/null http://example.com/index.html 2>&1 |
    sed -e 's|^.*(\([0-9.]\+ [KM]B/s\)).*$||'

回答by ennuikiller

Why can't you just do this:

为什么你不能这样做:

perl -ne "/^Downloaded.*?\((.*?)\)/; print "

回答by ghostdog74

here's suggestion. You can make use of wget's--limit-rate=amountoption. For example,

这是建议。您可以使用wget's--limit-rate=amount选项。例如,

--limit-rate=400kwill limit the retrieval rate to 400KB/s. Then its easier for you to calculate the total speed. Saves you time and mental anguish trying to regex it.

--limit-rate=400k将检索速率限制为 400KB/s。这样您就可以更轻松地计算总速度。节省您尝试正则表达式的时间和精神痛苦。

回答by Tim

This works when only 1 file is being downloaded.

这在仅下载 1 个文件时有效。

I started using sedto get the speed from wget, but I found it irritating so I switched to grep.

我开始使用sedwget 来获取速度,但我发现它很烦人,所以我切换到了 grep。

This is my command:

这是我的命令:

wget ... 2>&1 | grep -o "[0-9.]\+ [KM]*B/s"

The -ooption means it only returns that part. It matches 1 or more of the 10 digits then a space. Then optionally Kor Mbefore the B/s

-o选项意味着它只返回那部分。它匹配 10 个数字中的 1 个或多个,然后匹配一个空格。然后可选KMB/s

That will return 423 KB/s(for example).

这将返回423 KB/s(例如)。

To grep for just the units, use grep -o "[KM]*B/s"and for just the number use grep -o "[0123456789]\+.

要仅对单位进行 grep,请使用grep -o "[KM]*B/s"并且仅对数字使用grep -o "[0123456789]\+