LinuxQuestions.org
Visit Jeremy's Blog.
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 04-27-2011, 09:13 AM   #1
iddarth.85
LQ Newbie
 
Registered: Apr 2011
Location: India
Posts: 6

Rep: Reputation: 0
Number series in for loop in specific format


Hi,

I have a requirement that i will pass a number as input to the script and i need output as below.

Input: 4

Expected Output: WEEK04, WEEK05, WEEK06

For this i have the below script which giving the output without ZERO.

Actual Output: WEEK4, WEEK5, WEEK6

Script used:
-----------------------------------
i=$1
j=1
for j in {01..3..01}
do
echo WEEK$j=WEEK$i
i=`expr $i + 1`
done
-----------------------------------

Also please consider the below scenario

Input: 9

Expected Output: WEEK09, WEEK10, WEEK11


Can anyone please help me get desired output.
 
Old 04-27-2011, 09:28 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Bash strips the leading zero when you use normal outputting (print or echo), you need to use printf (formatted printing) to accomplish this.

Based on your original post:
Code:
#!/bin/bash

i=$1
j=1

for j in {1..3}
do
  printf "Week %02d\n" $i
  let i++
done
Hope this helps.

EDIT: Here's a link to an online printf page, which explains it all: The printf command

Last edited by druuna; 04-27-2011 at 09:34 AM.
 
Old 04-27-2011, 09:37 AM   #3
iddarth.85
LQ Newbie
 
Registered: Apr 2011
Location: India
Posts: 6

Original Poster
Rep: Reputation: 0
Wink

thanks a lot druuna...
it was very much helpful....

but i have to enhancemwnt in that.

I am getting the values, which i need to capture in variables.

Exmple:
WEEK1=Week09
WEEK2=Week10
WEEK3=Week11

where WEEK1, WEEK2, WEEK3 are the variable names which will be static always and Week09, Week10, Week10 are the values in case we pass the value to the script as "9".

Last edited by iddarth.85; 04-27-2011 at 09:46 AM.
 
Old 04-27-2011, 09:40 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You're welcome

BTW: Can you put up the [SOLVED] tag (first post -> Thread Tools).
 
Old 04-27-2011, 09:51 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I would probably make your WEEK variable an array ... it would make the scripting trivial.
 
Old 04-27-2011, 10:18 AM   #6
iddarth.85
LQ Newbie
 
Registered: Apr 2011
Location: India
Posts: 6

Original Poster
Rep: Reputation: 0
@druuna, can you please help me in gettin the output as described above in my previous post....

anyway posting it again....
---------------------------------------------
Exmple:
WEEK1=Week09
WEEK2=Week10
WEEK3=Week11
---------------------------------------------
 
Old 04-27-2011, 10:56 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Not sure if I completely understand, are you looking for something like this:
Code:
#!/bin/bash

i=$1
j=1

for j in {1..3}
do
  printf "Week%02d=" $j
  printf "Week%02d\n" $i
  let i++
done
This would generate the following (using 9 as input):
Code:
Week01=Week09
Week02=Week10
Week03=Week11
I did precede the static part with a zero, if that is not needed, change the first printf line to:
Code:
printf "Week%d=" $j
Hope this helps.
 
Old 04-27-2011, 10:58 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
As suggested:
Code:
i=$1

for j in {1..3}
do
    WEEK[j]=$(printf "Week%02d" $i)
    ((i++))
done

echo ${WEEK[*]}
 
1 members found this post helpful.
Old 04-27-2011, 02:20 PM   #9
speedy64
LQ Newbie
 
Registered: Apr 2011
Location: Kentucky
Distribution: Ubuntu 10.10
Posts: 7

Rep: Reputation: 0
I've tried a more straight forward approach. No loop.
Code:
#!/usr/bin/ksh

typeset -Z2 i

i=$1      ; week1=week$i
let i=i+1 ; week2=week$i
let i=i+1 ; week3=week$i

echo week1=$week1
echo week2=$week2
echo week3=$week3

Last edited by speedy64; 04-28-2011 at 12:27 AM.
 
Old 04-27-2011, 09:39 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Hey speedy ... please use [code][/code] tags when supplying code, for readability and preserve indentation.
 
Old 04-28-2011, 12:28 AM   #11
speedy64
LQ Newbie
 
Registered: Apr 2011
Location: Kentucky
Distribution: Ubuntu 10.10
Posts: 7

Rep: Reputation: 0
Thanks Grail. This is my first post anywhere EVER.
 
Old 04-28-2011, 01:41 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
No probs As you can see it is just a lot easier on the eyes.
 
Old 04-29-2011, 03:00 AM   #13
iddarth.85
LQ Newbie
 
Registered: Apr 2011
Location: India
Posts: 6

Original Poster
Rep: Reputation: 0
thans druuna, grail and speedy 64...
i have my question solved...

my requirement changed that i need 13 variables with each having 1 value in incremental fashion with the initial value being passed as a parameter.

after seeing your posts and with little tweaking i was able to achieve what i wanted...

please find teh code below:
<code>
i=$FSCL_WK_NBR
for j in {1..13..1}
do
wk[$j]=$(printf "week%02d" $i)
export Week[j]=${wk[j]}
i=`expr $i + 1`
#echo "Week$j=${wk[j]}"
done
</code>
 
Old 04-29-2011, 06:16 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
See post #10 for correct code tags.

Is there a purpose to exporting the variables?
 
  


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
sed replacing a specific character with a specific number ieatbunnies Linux - Newbie 2 11-04-2010 11:14 AM
Using awk to grab first number series buee Linux - Newbie 1 02-03-2010 05:08 PM
[SOLVED] BASH Question: Using a loop to download a series of files roystonlodge Programming 2 02-02-2010 03:18 PM
how to increase the loop devices number baloon Red Hat 3 03-29-2007 08:53 PM
Number series generator sniff Programming 8 08-23-2005 04:26 AM


All times are GMT -5. The time now is 05:14 AM.

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