Strip characters from cURL command output

3k views Asked by At

I am looking to take the download progress of a cURL command by taking only the first few characters of its progress bar output. Normally, I would use ${string:position:length}, but that doesn't seem to work in this situation.

Here's what I'm working with:

curl -O https://file.download.link/ > output.txt 2>&1

As you can see, I'm redirecting the output of the cURL command to the file output.txt, but let's say I want to only store the first three characters. Using what I just suggested returns a 'bad substitution' error:

echo ${curl -O https://file.download.link/:0:3} > output.txt 2>&1

so I'm out of my depth here.

If you'd like some more context, I was hoping to then change the command to output to a named pipe, so that it would change the progress of a CocoaDialog progress bar. I'm basically giving a GUI representation of the cURL download progress bar.

I would really appreciate any help or advice you could offer, so thank you in advance.

... and my apologies if this is a 'bad' question. I'm fairly new to bash, and scripting in general for that matter.

1

There are 1 answers

11
John1024 On

Here are two methods to get the first three characters of every line/update that curl produces. Note that, after curl prints its header and first output line, each subsequent line/update of output is preceded not by a newline character but by a carriage return, \r. On a terminal, this gives the output its nice update-in-place look. In our case, we have to add, as shown below, a little bit of special handling to interpret the \r correctly.

Using tr and grep

curl -O https://file.download.link/ 2>&1 |  tr '\r' '\n' | grep -o '^...' >output.txt

Using awk

curl -O https://file.download.link/ 2>&1 | awk -v RS='\r' '{print substr($0,1,3)}' >output.txt

Sample Output

$ curl -O http://www.google.com/index.html 2>&1 | awk -v RS='\r' '{print substr($0,1,3)}'
  %
  0
100