在csv文件中下载许多URL并另存为Linux

在csv文件中下载许多URL并另存为Linux

本文介绍了在csv文件中下载许多URL并另存为Linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv文件,该文件包含两列:用于图像链接的A列和用于名称的B列.第一和第二列用逗号分隔.我需要下载A列中的所有文件,并在B列中为其分配名称.我尝试了以下语法:

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'

我是bash的初学者,对此有什么帮助吗?

I am beginner with bash, any help with this?

推荐答案

有多种选择,例如使用awk处理带有诸如CSV之类的字段分隔符的文件.

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:

遍历您的CSV文件(同时)

获取每一行字段(IFS用于将,"设置为字段分隔符)

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

将其与wget -O选项一起使用以指定文件名

例如像这样的东西:

#!/bin/bash

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

编辑.只需复制粘贴您的代码片段(在while循环中缺少正确的标识即可.),并且可以正常工作

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 ?

我将其保存在"test.sh"中并像这样启动它,在同一文件夹中有"urls.csv"文件:

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

./test.sh

这篇关于在csv文件中下载许多URL并另存为Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 23:35