views:

1161

answers:

2

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.

A: 

You need to use .* not * in your regex.

grep -o '.*[0-9].*'

That will catch all text though, so maybe try:

grep -p '[0-9]+'
samoz
Do I need to further pipe this into echo to actually see the results of the grep?
Abs
+1  A: 

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%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Mark Rushakoff
Ah I see, redirect output so I can actually see it. I was hoping just to extract the numbers as they appear like you said (the former). I tried the above but I only get output when it reaches 100% - maybe I need a larger file to actually see the progress change?
Abs
Curl's output is meant to be read by humans, not by computers -- that's why the format sucks when taken in by grep. You might have better luck using `wget --progress=dot` and counting the dots, but then it's not a percentage either. Or maybe you would be able to just count the #'s in curl's output. At least you'd get accuracy to 5-10%, or however many #'s fit in a terminal, right?
Mark Rushakoff
You are right, Probably better luck counting the hashes. I can't use wget since this is blocked by my host for some reason. A previous question I asked had the consensus that CURL is the next best choice if you can't use wget. Should of added which of the other options has an easy way of parsing the progress too!!
Abs