2016-07-22 87 views
1

我想做並行下載,但問題wget輸出不正確的文件名。xargs wget從URL提取文件名與參數

url.txt

http://example.com/file1.zip?arg=tereef&arg2=okook 
http://example.com/file2.zip?arg=tereef&arg2=okook 

命令

xargs -P 4 -n 1 wget <url.txt 

輸出文件名

file1.zip?arg=tereef&arg2=okook 
file2.zip?arg=tereef&arg2=okook 

預期輸出

file1.zip 
file2.zip 

我是新的bash,請告訴我如何輸出正確的文件名,並且請不要建議for loop或&因爲它阻塞。

謝謝

+0

使用'-O file'外面可以看到一個bash功能開關設置'wget'中的輸出文件名。 – xxfelixxx

回答

0

您可以使用,你必須導出當前外殼

function mywget() 
{ 
    wget -O ${1%%\?*} "'$1'" 
} 
export -f mywget 
xargs -P 4 -n 1 -I {} bash -c "mywget '{}'" < url.txt 
+0

它的工作謝謝,但我必須用'$(basename $ {1 %% \?*})'替換'$ {1 %% \?*}',因爲它會返回'http://example.com/file1.zip' – uingtea

+0

是的,我忘記了 –

0

處理您輸入到生產所需的命令,然後通過xargs的運行它。

perl -ne - 遍歷輸入文件的行並執行內嵌的程序

-e:執行Perl的一行

-n:遍歷所有的行,每個分配到$ _ 反過來。

xargs -P 4 -n 1 -i -t wget "{}"

-P 4:4個進程最大一次

-n 1:消耗每次一個輸入線

-i:使用替換字符串「{}」

-t:在執行之前打印命令

perl -ne ' 
    chomp(my ($url) = $_);       # Remove trailing newline 
    my ($name) = $url =~ m|example.com/(.+)\?|; # Grab the filename 
    print "$url -O $name\n";      # Print all of the wget params 
' url.txt | xargs -P 4 -n 1 -i -t wget "{}" 

輸出

wget http://example.com/file1.zip?arg=tereef&arg2=okook -O file1.zip 
wget http://example.com/file2.zip?arg=tereef&arg2=okook -O file2.zip 
--2016-07-21 22:24:44-- http://example.com/file2.zip?arg=tereef&arg2=okook%20-O%20file2.zip 
--2016-07-21 22:24:44-- http://example.com/file1.zip?arg=tereef&arg2=okook%20-O%20file1.zip 
Resolving example.com (example.com)... Resolving example.com (example.com)...  93.184.216.34, 2606:2800:220:1:248:1893:25c8:1946 
93.184.216.34, Connecting to example.com (example.com)|93.184.216.34|:80... 2606:2800:220:1:248:1893:25c8:1946 
Connecting to example.com (example.com)|93.184.216.34|:80... connected. 
connected. 
HTTP request sent, awaiting response... HTTP request sent, awaiting response... 404 Not Found 
2016-07-21 22:24:44 ERROR 404: Not Found. 

404 Not Found 
2016-07-21 22:24:44 ERROR 404: Not Found. 
+0

感謝您的回答 – uingtea