Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I iterate through lines of a remote file in shell using cURL?

Tags:

bash

shell

curl

I understand how to iterate through lines in a local file. But how about a remote file?

while read NAME
do
    echo "$NAME"
done < curl -sL 'http://mylistofnames.com/list.html

If this were local, I would replace the last line with done < names.txt. I'm stuck on remote.

like image 469
Ryan Avatar asked Mar 19 '23 23:03

Ryan


2 Answers

Put the curl command in a bash process substitution:

while read NAME
do
    echo "$NAME"
done < <(curl -sL 'http://mylistofnames.com/list.html')

Piping works as well, but if variables are set in the while loop and they are needed outside the scope of the while loop then this solution becomes necessary.

like image 111
Digital Trauma Avatar answered Apr 08 '23 16:04

Digital Trauma


Pipe the output:

curl -sL 'http://mylistofnames.com/list.html' | while read NAME
    do echo "$NAME"
done

With process substitution:

while read NAME
    do echo "$NAME"
done < <(curl -sL 'http://mylistofnames.com/list.html')
like image 30
Barmar Avatar answered Apr 08 '23 16:04

Barmar