LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-11-2012, 03:18 AM   #1
aggrishabh
Member
 
Registered: Nov 2010
Posts: 87

Rep: Reputation: 1
print counting using for loop


Hi All,

i need to print countiong using for loop like below

001
002
003
004
.
.
.
009
010
.
.
.
999

i tried
Code:
for i in {001..999}
do
print $i
done
but its printing in simple format
1
2
3
.
.
.
9
10
.
.
.

may me the simple one. please help
 
Old 01-11-2012, 03:28 AM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 190Reputation: 190
printf will allow you to do it.

Code:
    for ((i=1; i<=999; i++))
    do
        printf "%03d \n" "$i"
    done
"%d" will display a decimal integer, specified by the variable $i. The "0", is the character to lead with, and the 3 is the number of leading characters.

Last edited by fukawi1; 01-11-2012 at 03:29 AM.
 
Old 01-11-2012, 03:46 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
not sure what language this is, but in bash you could do

Code:
for i in `seq -f "%03g" 1 999`
do
   echo $i
done
 
Old 01-11-2012, 11:20 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Zero-padding in brace expansion was introduced in bash version 4. But since you used "print" and not "echo" in your example, it appears that you may be using ksh or another shell instead (and that's something you really should mention when you post). I'm not sure about the status in those other shells.

For more on zero-padding in bash, see here. It also includes a brief mention about using ksh's typeset.

http://mywiki.wooledge.org/BashFAQ/018
 
Old 01-15-2012, 10:33 AM   #5
aggrishabh
Member
 
Registered: Nov 2010
Posts: 87

Original Poster
Rep: Reputation: 1
Thank you all for your valuable inputs.
 
  


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
[SOLVED] AWK; print array in loop no output cristalp Programming 6 12-12-2011 03:14 PM
for loop to iterate four times, print , then start again casperdaghost Programming 1 07-24-2011 03:19 PM
[SOLVED] [bash] while loop ..always print the first variable. ridwanfi Programming 5 06-10-2011 09:12 AM
assigning column name to awk print in a loop bashprog Programming 5 09-29-2010 02:54 AM
Using a for loop to print an interest compounding table guitarstorm Linux - Newbie 4 03-01-2009 12:46 PM


All times are GMT -5. The time now is 01:36 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration