0

When I run

curl https://example.com -o example.html 2>&1 | grep -P "\d.*\d"

I get this output (in red):

100  1270  100  1270    0     0    318      0  0:00:04  0:00:04 0:00:00   318


What I want is just to show the last line of cURL's progress bar as it updates (I actually only want certain columns to be shown, but showing the whole line is fine)


Wanted output (at time t = 1 sec):

user@comp:~/Desktop/$ curl https://example.com -o example.html 2>&1 | grep -P "\d.*\d"
25  1270  25  318    0     0    318      0  0:00:01  0:00:01 0:00:03   318

Wanted output (at time t = 2 sec):

user@comp:~/Desktop/$ curl https://example.com -o example.html 2>&1 | grep -P "\d.*\d"
50  1270  50  636    0     0    318      0  0:00:02  0:00:02 0:00:02   318

Wanted output (at time t = 3 sec):

user@comp:~/Desktop/$ curl https://example.com -o example.html 2>&1 | grep -P "\d.*\d"
75  1270  75  954    0     0    318      0  0:00:03  0:00:03 0:00:01   318

Wanted output (at time t = 4 sec):

user@comp:~/Desktop/$ curl https://example.com -o example.html 2>&1 | grep -P "\d.*\d"
100  1270  100 1270    0     0    317      0  0:00:04  0:00:04 0:00:00   0

I've tried using watch with cURL and grep, but it still doesn't work (no output)

watch --no-title --interval 1 "curl http://example.com -o test2.html" | grep -P '\d.*\d'
0

You cannot directly | grep the output of curl progress because of pipe buffering. Some great insights on this issue is available in this previous question: Turn off buffering in pipe

As for a solution, using stdbuf and tr:

curl https://example.com -o example.html 2>&1 | stdbuf -oL tr -s '\r' '\n' | while read i; do echo -en "\r$i  "; done

With:

  • stdbuf -oL: stdout of tr will be line buffered for proper processing by the while loop
  • tr -s '\r' '\n': replace carriage return with newline from curl output
  • while read i; do echo -en "\r$i "; done: simple bash solution for a progress line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.