简体   繁体   English

Bash,wget从输出文件名中删除逗号

[英]Bash, wget remove comma from output filename

I am reading a file with URL's by line by line then I pass URL to the wget: 我正在逐行读取带有URL的文件,然后将URL传递给wget:

FILE=/home/img-url.txt
while read line; do
url=$line
wget -N -P /home/img/ $url
done < $FILE

This works, but some file contains comma in the filename. 这有效,但有些文件在文件名中包含逗号。 How I can save the file without the comma? 如何在没有逗号的情况下保存文件?

Example: 例:

http://xy.com/0005.jpg -> saved as 0005.jpg
http://xy.com/0022,22.jpg -> save as 002222.jpg not as 0022,22

I hope you find my question interesting. 我希望你发现我的问题很有意思。

UPDATE: 更新:

We have some nice solution, but is there any solution to the time stamping error? 我们有一些很好的解决方案,但有时间戳错误的解决方案吗?

WARNING: timestamping does nothing in combination with -O. See the manual
for details.

In the body of the loop you need to generate the filename from the URL without commas and, without the leading part of the URL, and tell wget to save under other name. 在循环体中,您需要从URL生成文件名而不使用逗号,并且不需要URL的前导部分,并告诉wget以其他名称保存。

url=$line
file=`echo $url | sed -e 's|^.*/||' -e 's/,//g'`
wget -N -P /home/image/dema-ktlg/ -O $file $url

This should work: 这应该工作:

url="$line"
filename="${url##*/}"
filename="${filename//,/}"
wget -P /home/img/ "$url" -O "$filename"

Using -N and -O both will throw a warning message. 使用-N和-O都将发出警告消息。 wget manual says: wget手册说:

-N (for timestamp-checking) is not supported in combination with -O: since file is always newly created, it will always have a very new timestamp. -N(用于时间戳检查)不支持与-O组合使用:因为文件总是新创建的,所以它总是会有一个非常新的时间戳。

So, when you use -O option, it actually creates a new file with new timestamping and thus the -N option becomes dummy (it can't do what it is for). 因此,当您使用-O选项时,它实际上会创建一个带有新时间戳的新文件,因此-N选项变为虚拟(它无法完成它的工作)。 If you want to preserve the timestapming, then a workaround might be this: 如果您想保留时间戳,那么解决方法可能是这样的:

url="$line"
wget -N -P /home/img/ "$url"
file="${url##*/}"
newfile="${filename//,/}"
[[ $file != $newfile ]] && cp -p /home/img/"$file" /home/img/"$newfile" && rm /home/img/"$file"

In the meantime I wrote this: 与此同时,我写道:

url=$line
$file=`echo ${url##*/} | sed 's/,//'`
wget -N -P /home/image/dema-ktlg/ -O $file $url

Seems to work fine, is there any trivial problem with my code? 似乎工作正常,我的代码有任何微不足道的问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM