I have a csv-file that looks like this (short example, there are actually about 20 images per id).


The structure is like this:


The bold version is repeated as many times as the source xml has these fields, so there can be (most likely is) different amount of images per id. I need to create a folder and download these images in that folder based on the id, for example


There are also image files that are thumbnails and "www-sized", but I only need the ones that have kivirealty-original as a field before the image location.

How can I parse the files I need and then wget those files to a folder that is named by it's id?

The example file is generated by a shell script, and I would need a code that would do this after that file is done.

  • You can parse CSV file with Perl or Python. Since CSV files can have newlines in cell-content, using line oriented tools is a shortcut only beginners try to take. Once you have the cell contents call wget or use the language build in retrieval tools. – Anthon Feb 10 '15 at 11:51
  • Thanks for the help, however I'm in a need for bit more detailed examples. I am fairly new to shell scripting. I have been using awk to get only the main images to use as preview pictues by looking for value pääkuva;1;kivirealty-original;1;, but now I need to download them all (not just pääkuva = main image) and in different folders. – SamSwampwater Feb 10 '15 at 12:00
while IFS=";" read id a remain
    while [ "$remain" ]
        IFS=";" read a b test d i_url remain <<< "$remain"
        if [ "$test" == "kivirealty-original" ]
            if [ ! -d "some_path/$id" ]
                mkdir -p "some_path/$id"
            wget -nd -P "some_path/$id" "$i_url"
done < file.csv
  • This did just what I needed, thank you very much! – SamSwampwater Feb 10 '15 at 14:28

I'd use awk to create the commands and pipe the output into sh if all works as desired. Here's a code skeleton (you'll have to add the desired wget options)...

awk -F\; '
    printf "mkdir -p %s/%s\n", "somepath", $1
    for (i=7; i<=NF; i+=5) {
        printf "wget ... %s ... %s/%s ...\n", $i, "somepath", $1
' | sh

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.