LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Echo sequence of numbers with '0' padding for length of 3 chars in CLI (https://www.linuxquestions.org/questions/programming-9/echo-sequence-of-numbers-with-0-padding-for-length-of-3-chars-in-cli-769259/)

koobi 11-15-2009 04:05 AM

Echo sequence of numbers with '0' padding for length of 3 chars in CLI
 
I'm trying to run this command directly via CLI instead of via a shell script.

This is what I have:
Code:

$ wget http://www.example.org/module_{1..190}.so

That would generate something like:
Code:

http://www.example.org/module_1.so
http://www.example.org/module_2.so
.
.
http://www.example.org/module_9.so
http://www.example.org/module_10.so
.
.
http://www.example.org/module_190.so

etc.


But, the problem is, files are named like this:
Code:

module_001.so
module_002.so
.
.
module_009.so
module_010.so
.
.
module_190.so


How would I do that?
Thanks.

pixellany 11-15-2009 04:31 AM

Google foound a lot of stuff on zero-padding.

e.g. try "seq -w"

as in:
Code:

for num in $(seq -w 1 10);do
    echo "number is "$num
done


ghostdog74 11-15-2009 05:22 AM

so why can't you do like this?
Code:

echo {000..190}

H_TeXMeX_H 11-15-2009 05:57 AM

I know this works:

Code:

printf "%03d\n" {1..190}
So:

Code:

printf "http://www.example.org/module_%03d.so\n" {1..190}

koobi 11-15-2009 02:19 PM

I probably should have mentioned that I've also forgotten how to quote the command so that it's not interpreted literally in the one liner.

I did try out seq (but without the -w switch) but how do I quote it into my example?

I also like the clean printf solution but I really don't remember how to quote it in.

This obviously won't work:
Code:

wget printf "http://www.doublefine.com/themes/site_themes/default/images/comics/sc/sc_%03d.jpg" {1..190}
Using wget's -e switch also throws an error.
Unfortunately, I need you to spoon feed me and tell me exactly how I can use it.
I really should re-learn my shell commands.

H_TeXMeX_H 11-15-2009 02:36 PM

well, what I would do is:

Code:

printf "http://www.doublefine.com/themes/site_themes/default/images/comics/sc/sc_%03d.jpg\n" {1..190} > get
wget -i get

it's easier like that. If you don't like that, then you can just do:

Code:

printf "http://www.doublefine.com/themes/site_themes/default/images/comics/sc/sc_%03d.jpg\n" {1..190} | while read line; do wget "$line"; done

koobi 11-15-2009 02:54 PM

I prefer the second solution. Thanks a lot :)

Isn't it possible to do something similar to:
Code:

wget http://example.org/foo{1..5}.bar
I remember doing something similar ages ago...maybe it was in Perl?

Kenhelm 11-15-2009 06:46 PM

curl is better than wget for this because it has a built in ability to create sequences.
From the curl man page:
Code:

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


H_TeXMeX_H 11-16-2009 04:14 AM

Quote:

Originally Posted by Kenhelm (Post 3758274)
curl is better than wget for this because it has a built in ability to create sequences.
From the curl man page:
Code:

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


Indeed, if you wanted to use curl instead of wget you can do:

Code:

curl -o sc_#1.jpg http://www.doublefine.com/themes/site_themes/default/images/comics/sc/sc_[001-190].jpg

bigearsbilly 11-16-2009 04:40 AM

if you use the (IMHO) superior korn shell you can use typeset

observe:
Code:

$ typeset -Z3 a
$ a=1
$ echo $a
001



All times are GMT -5. The time now is 08:04 PM.