问题
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