LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-15-2009, 04:05 AM   #1
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Rep: Reputation: 15
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.
 
Old 11-15-2009, 04:31 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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
 
Old 11-15-2009, 05:22 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
so why can't you do like this?
Code:
echo {000..190}
 
Old 11-15-2009, 05:57 AM   #4
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
I know this works:

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

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

Last edited by H_TeXMeX_H; 11-15-2009 at 06:07 AM.
 
Old 11-15-2009, 02:19 PM   #5
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Original Poster
Rep: Reputation: 15
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.
 
Old 11-15-2009, 02:36 PM   #6
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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
 
Old 11-15-2009, 02:54 PM   #7
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Original Poster
Rep: Reputation: 15
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?
 
Old 11-15-2009, 06:46 PM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
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
 
Old 11-16-2009, 04:14 AM   #9
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Quote:
Originally Posted by Kenhelm View Post
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
 
Old 11-16-2009, 04:40 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,284

Rep: Reputation: 172Reputation: 172
if you use the (IMHO) superior korn shell you can use typeset

observe:
Code:
$ typeset -Z3 a
$ a=1
$ echo $a
001
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing a numeric string with a sequence of numbers ziggy25 Linux - Newbie 13 12-03-2009 10:18 AM
[SOLVED] regex with numbers in sequence schneidz Programming 6 04-28-2009 06:52 PM
bash: How to echo plenty of ' and " chars ? frenchn00b Programming 8 12-29-2008 02:50 AM
LXer: Predicting Solaris 10 TCP Sequence Numbers Part 1: Initial Discovery LXer Syndicated Linux News 0 12-02-2008 01:10 PM
sequence of numbers, how to extract which numbers are missing jonlake Programming 13 06-26-2006 03:28 AM


All times are GMT -5. The time now is 12:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration