LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Number series in for loop in specific format (https://www.linuxquestions.org/questions/linux-newbie-8/number-series-in-for-loop-in-specific-format-877390/)

iddarth.85 04-27-2011 08:13 AM

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.

druuna 04-27-2011 08:28 AM

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

iddarth.85 04-27-2011 08:37 AM

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".

druuna 04-27-2011 08:40 AM

You're welcome :)

BTW: Can you put up the [SOLVED] tag (first post -> Thread Tools).

grail 04-27-2011 08:51 AM

I would probably make your WEEK variable an array ... it would make the scripting trivial.

iddarth.85 04-27-2011 09:18 AM

@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
---------------------------------------------

druuna 04-27-2011 09:56 AM

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.

grail 04-27-2011 09:58 AM

As suggested:
Code:

i=$1

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

echo ${WEEK[*]}


speedy64 04-27-2011 01:20 PM

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


grail 04-27-2011 08:39 PM

Hey speedy ... please use [code][/code] tags when supplying code, for readability and preserve indentation.

speedy64 04-27-2011 11:28 PM

Thanks Grail. This is my first post anywhere EVER.

grail 04-28-2011 12:41 AM

No probs :) As you can see it is just a lot easier on the eyes.

iddarth.85 04-29-2011 02:00 AM

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>

grail 04-29-2011 05:16 AM

See post #10 for correct code tags.

Is there a purpose to exporting the variables?


All times are GMT -5. The time now is 08:37 AM.