0

Let's say I have a .txt file where I have a list of image links that I want to download. exaple:

image.jpg
image2.jpg
image3.jpg

I use: cat images.txt | xargs wget and it works just fine

What I want to do now is to provide another .txt file with the following format:

some_id1:image.jpg
some_id2:image2.jpg
some_id3:image3.jpg

What I want to do is to split each line in the ':' , download the link to the right, and change the downloaded file-name with the id provided to the left.

I want to somehow use wget image.jpg -O some_id1.jpg

So the output will be:

some_id1.jpg
some_id2.jpg
some_id3.jpg

Any ideas ?

Giorgos Perakis
  • 153
  • 2
  • 12
  • Possible duplicate of [Looping over pairs of values in bash](http://stackoverflow.com/questions/28725333/looping-over-pairs-of-values-in-bash) – tripleee Jul 04 '16 at 12:45

1 Answers1

0

My goto for such tasks is awk:

while read line; do lfn=`echo "$line" | awk -F":" '{ print $1".jpg" }'` ; rfn=`echo "$line" | awk -F":" '{ print $2 }'` ; wget $rfn -O $lfn ; done < images.txt

This presumes, of course, all the local file names should have the .jpg extension.

Jef
  • 1,128
  • 9
  • 11