Download many URLs in csv file and save as in Linux [closed]

泪湿孤枕 提交于 2020-04-18 05:44:34

问题


I have a csv file which has two columns: column A for image links and column B for names. First and second columns are comma separated. I need to download all files in column A and assign them names in column B. I have tried the syntax below:

#!/bin/bash

while IFS=',' read -r url filename
do
wget -O $filename $url
done < urls.csv

However, I got these errors:

line 2: $'\r': command not found
line 6: syntax error near unexpected token `done'
line 6: `done < urls.csv'

I am beginner with bash, any help with this?


回答1:


There are several alternatives, for example using awk, to process a file with field separator characters like a CSV.

However, I will try to KISS this specific case:

Iterate through your CSV file (while)

Get each line fields (IFS is used to set ',' as field separator)

Use them with wget -O option to specify a filename

e.g. something like this:

#!/bin/bash

while IFS=',' read -r url filename
do
    wget -O $filename $url
done < yourfile.csv

edit. Just copy pasted your snippet (which lacks proper identation inside the while loop..), and works properly

Perhaps you could share how are you executing that snippet ?

I'm saving it in 'test.sh' and launching it like this, having "urls.csv" file in same folder:

./test.sh


来源:https://stackoverflow.com/questions/60603190/download-many-urls-in-csv-file-and-save-as-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!