CURL 进度条:如何仅使用 grep 管道和提取数字? [英] CURL Progress Bar: How to pipe and extract numbers only using grep?

查看:20
本文介绍了CURL 进度条:如何仅使用 grep 管道和提取数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前所拥有的:

[my1@graf home]$ curl -# -o f1.flv 'http://osr.com/f1.flv' |grep -o '*[0-9]*'#################################################################### 100.0%

我希望使用 grep 并且只从 CURL 输出的进度条中提取百分比.

我认为我的正则表达式不正确,我也不确定这个 grep 是否会影响不断更新的百分比?

我想要做的基本上是让 CURL 只给我百分比数字作为输出,没有别的.

感谢您的帮助.

解决方案

你无法通过 grep 那样获取进度信息;没有意义.

curl 将进度条写入 stderr,因此您必须先重定向到 stdout,然后才能 grep:

$ curl -# -o f1.flv 'http://osr.com/f1.flv' 2>&1 |1 |减少 结果:

<前>^M 0.0%^M################################################################### 100.0%^M################################################################### 100.0%^M################################################################### 100.0%

您是否希望将连续的数字流重定向到其他地方?或者您希望在一个点上获取数字?

如果是前者,这种半途而废的工作在一个小文件上:

$ curl -# -o f1.flv 'http://osr.com/f1.flv' 2>&1 |sed 's/#//g' -100.0% 0.0%

但它在大文件上没用.在下载完成之前不会打印输出,可能是因为 curl 似乎将 ^H 发送到终端.可能有更好的方法来sed它,但我不会屏住呼吸.

$ curl -# -o l.tbz 'ftp://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/2009/06/2009-06-02-05-mozilla-1.9.1/firefox-3.5pre.en-US.linux-x86_64.tar.bz2' 2>&1 |sed 's/#//g' -100.0%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

This is what I have so far:

[my1@graf home]$ curl -# -o f1.flv 'http://osr.com/f1.flv' |  grep -o '*[0-9]*'
####################################################################### 100.0%

I wish to use grep and only extract the percentage from that progress bar that CURL outputs.

I think my regex is not correct and I am also not sure if this grep will take effect of the the percentage being continuously updated?

What I am trying to do is basically get CURL only to give me the percentage number as the output and nothing else.

Thank you for any help.

解决方案

You can't get the progress info like that through grep; it doesn't make sense.

curl writes the progress bar to stderr, so you have to redirect to stdout before you can grep it:

$ curl -# -o f1.flv 'http://osr.com/f1.flv' 2>&1 | grep 1 | less results in:

^M                                                                           0.0
%^M######################################################################## 100.
0%^M######################################################################## 100
.0%^M######################################################################## 10
0.0%

Are you expecting a continual stream of numbers that you are redirecting somewhere else? Or do you expect to grab the numbers at a single point?

If it's the former, this sort of half-assedly works on a small file:

$ curl -# -o f1.flv 'http://osr.com/f1.flv' 2>&1 | sed  's/#//g' -
 100.0%                                                                    0.0%

But it's useless on a large file. The output doesn't print until the download is finished, probably because curl seems to be sending ^H's to the terminal. There might be a better way to sed it, but I wouldn't hold my breath.

$ curl -# -o l.tbz 'ftp://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/2009/06/2009-06-02-05-mozilla-1.9.1/firefox-3.5pre.en-US.linux-x86_64.tar.bz2' 2>&1 | sed 's/#//g' -
 100.0%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

这篇关于CURL 进度条:如何仅使用 grep 管道和提取数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆