BASHスクリプト:連続した番号付きファイルをwgetでダウンロードする

StackOverflow https://stackoverflow.com/questions/1426522

  •  07-07-2019
  •  | 
  •  

質問

番号の付いたWebアプリケーションのログファイルを保存するWebサーバーがあります。このファイル名の例は次のとおりです。

dbsclog01s001.log
dbsclog01s002.log
dbsclog01s003.log

最後の3桁はカウンターであり、最大100まで取得できます。

通常、Webブラウザを開き、次のようなファイルを参照します。

http://someaddress.com/logs/dbsclog01s001.log

ファイルを保存します。もちろん、50個のログを取得すると、少し面倒です。 wgetを使用して渡すためのBASHスクリプトを考案しようとしました

http://someaddress.com/logs/dbsclog01s*.log

しかし、スクリプトに問題があります。 とにかく、これを行う方法のサンプルはありますか?

ありがとう!

役に立ちましたか?

解決

#!/bin/sh

if [ $# -lt 3 ]; then
        echo "Usage: 
$ ./seq_wget http://someaddress.com/logs/dbsclog01s%03d.log 1 50
url_format seq_start seq_end [wget_args]" exit fi url_format=$1 seq_start=$2 seq_end=$3 shift 3 printf "$url_format\\n" `seq $seq_start $seq_end` | wget -i- "$@"

上記を seq_wget として保存し、実行許可( chmod + x seq_wget )を付与してから実行します。例:

$ wget http://someaddress.com/logs/dbsclog01s{001..050}.log

または、Bash 4.0を使用している場合は、単に入力できます

<*>

または、 wget の代わりに curl がある場合は、デニスウィリアムソンの答えに従うことができます。

他のヒント

curl は範囲をサポートしているようです。 man ページから:

URL  
       The URL syntax is protocol dependent. You’ll find a  detailed  descrip‐
       tion in RFC 3986.

       You  can  specify  multiple  URLs or parts of URLs by writing part sets
       within braces as in:

        http://site.{one,two,three}.com

       or you can get sequences of alphanumeric series by using [] as in:

        ftp://ftp.numericals.com/file[1-100].txt
        ftp://ftp.numericals.com/file[001-100].txt    (with leading zeros)
        ftp://ftp.letters.com/file[a-z].txt

       No nesting of the sequences is supported at the moment, but you can use
       several ones next to each other:

        http://any.org/archive[1996-1999]/vol[1-4]/part{a,b,c}.html

       You  can  specify  any amount of URLs on the command line. They will be
       fetched in a sequential manner in the specified order.

       Since curl 7.15.1 you can also specify step counter for the ranges,  so
       that you can get every Nth number or letter:

        http://www.numericals.com/file[1-100:10].txt
        http://www.letters.com/file[a-z:2].txt

&quot;先行ゼロ付き&quot;と表示されていることに気付いたかもしれません!

wget URLでエコータイプシーケンスを使用して、数字の文字列をダウンロードできます...

wget http://someaddress.com/logs/dbsclog01s00 {1..3} .log

これは文字でも機能します

{a..z} {A..Z}

a forループi n bashとの組み合わせを使用できます。 printf コマンド(もちろん echo に変更する必要に応じてwget ):

$ for i in {1..10}; do echo "http://www.com/myurl`printf "%03d" $i`.html"; done
http://www.com/myurl001.html
http://www.com/myurl002.html
http://www.com/myurl003.html
http://www.com/myurl004.html
http://www.com/myurl005.html
http://www.com/myurl006.html
http://www.com/myurl007.html
http://www.com/myurl008.html
http://www.com/myurl009.html
http://www.com/myurl010.html

発生している問題を正確に判断することはできませんが、bashの単純なforループが問題を解決するように思えます。

for i in {1..999}; do
wget -k http://someaddress.com/logs/dbsclog01s$i.log -O your_local_output_dir_$i;
done

おもしろいタスクなので、私はあなたのために完全なスクリプトを書きました(いくつかの答えを組み合わせて)。ここにあります:

#!/bin/bash
# fixed vars
URL=http://domain.com/logs/     # URL address 'till logfile name
PREF=logprefix                  # logfile prefix (before number)
POSTF=.log                      # logfile suffix (after number)
DIGITS=3                        # how many digits logfile's number have
DLDIR=~/Downloads               # download directory
TOUT=5                          # timeout for quit
# code
for((i=1;i<10**$DIGITS;++i))
do
        file=$PREF`printf "%0${DIGITS}d" $i`$POSTF   # local file name
        dl=$URL$file                                 # full URL to download    
        echo "$dl -> $DLDIR/$file"                   # monitoring, can be commented
        wget -T $TOUT -q $dl -O $file
        if [ "$?" -ne 0 ]                            # test if we finished
        then
                exit
        fi
done

スクリプトの開始時に、URL、ログファイルのプレフィックスとサフィックス、ナンバリングパーツとダウンロードディレクトリの桁数を設定できます。ループは、見つかったすべてのログファイルをダウンロードし、最初に存在しないときに自動的に終了します(wgetのタイムアウトを使用)。

このスクリプトでは、例で述べたように、ログファイルのインデックス付けがゼロではなく1で始まることを前提としていることに注意してください。

これがお役に立てば幸いです。

ここでは、必要なものに見えるPerlスクリプトを見つけることができます

http://osix.net/modules/article/?id=677

#!/usr/bin/perl
$program="wget"; #change this to proz if you have it ;-)
my $count=1; #the lesson number starts from 1
my $base_url= "http://www.und.nodak.edu/org/crypto/crypto/lanaki.crypt.class/lessons/lesson";
my $format=".zip"; #the format of the file to download
my $max=24; #the total number of files to download
my $url;

for($count=1;$count<=$max;$count++) {
    if($count<10) {
    $url=$base_url."0".$count.$format; #insert a '0' and form the URL
    }
    else {
    $url=$base_url.$count.$format; #no need to insert a zero
    }
    system("$program $url");
}

'globbing'についてのwgetのマンページでの議論を見たところです:

デフォルトでは、URLにグロビング文字が含まれている場合、グロビングがオンになります。このオプションは、グロビングを永続的にオンまたはオフにするために使用できます。 シェルによって展開されないようにURLを引用する必要がある場合があります。 Globbingは、Wgetがシステム固有のディレクトリリストを検索するようにします。 これが現在Unix FTPサーバーでのみ動作する理由です(およびUnix&quot; ls&quot;出力をエミュレートするサーバー)

つまり、wget http:// ...はグロビングでは機能しません。

システムにseqがあるかどうかを確認すれば、簡単です:

for i in $(seq -f "%03g" 1 10); do wget "http://.../dbsclog${i}.log"; done

システムにseqの代わりにjotコマンドがある場合:

for i in $(jot -w "http://.../dbsclog%03d.log" 10); do wget $i; done

ああ!これは、マンガのダウンロードを自動化するためにbashを学習するときに遭遇した同様の問題です。

次のようなものが機能するはずです:

for a in `seq 1 999`; do
if [ ${#a} -eq 1 ]; then
    b="00"
elif [ ${#a} -eq 2 ]; then
    b="0"
fi
echo "$a of 231"
wget -q http://site.com/path/fileprefix$b$a.jpg

完了

パーティーに遅れましたが、コーディングを必要としない本当に簡単なソリューションは、ファイルの範囲を取得する機能を備えたDownThemAll Firefoxアドオンを使用することです。 800個の連続した番号のファイルをダウンロードする必要があるとき、それが私のソリューションでした。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top