Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i have a text file that contains 10,000 url's with a unique number i want to save the file as. Each line has a 10 character code, then the URL of the image to retrieve. How can I make the input file use the first 10 characters as the wget filename?

this is an example of the input file: input.txt


wget -i input.txt would get the file but not with the preceding unique number.

I want t29123q8m19.jpg (the first line) to be saved as x100083590.jpg

If there is a better way to write out the input file, say with the URL first, then I can do that too, but I will never know the length of the first field. Right now the first 10 characters will always be what I want to save the wget image as.

Edit This is being done in a windows environment.

share|improve this question
up vote 2 down vote accepted

Use the following batch file:

@echo off
setlocal enabledelayedexpansion
for /f %%l in (Input.txt) do (
    set line=%%l
    wget -O !line:~0,10!.jpg !line:~10!
share|improve this answer
This worked. Thank you Karan. – Matt Nov 20 '12 at 23:55

In linux.

 while read p; do
   newname=${p:0:10} # first 10 chars
   url=${p:10} # remaining chars after the 10th
   wget $url -O $newname.jpg  #get url and output to new filename
 done < input.txt

Under windows, we could do:

 for /f %%p in (input.txt) do (
    SET p1=$$p
    SET newname=!p1:~0,10!
    SET url=!p1:~10!
    wget %url% -O %newname%.jpg
share|improve this answer
Thank you for the Prompt reply Paul. How would one do it in Windows? – Matt Nov 20 '12 at 22:37
@Matt Added windows, let me know if you find any bugs – Paul Nov 20 '12 at 23:03
Here is the error i get upon running this in a batch file: --2012-11-20 15:19:44-- http://~10/ Resolving ~10... failed: No data record of requested type. wget: unable to resolve host address `~10' – Matt Nov 20 '12 at 23:24
@Matt Ok try this. Stupid windows :) – Paul Nov 20 '12 at 23:56
Same error. Karan's solution above is almost the same, and is working. I still thank you for your assistance and contribution. – Matt Nov 21 '12 at 0:08

Using and the shell (cygwin or git-bash):

awk '{print "wget \047" substr($0, 11) "\047 -o " substr($0, 0, 10) ".jpg"}' "$file | sh

same, but multi lines version:

awk '
        print "wget \047" substr($0, 11) "\047 -o " substr($0, 0, 10) ".jpg"
' "$file | sh
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .