increment counter as suffix starting with the rightmost digit

Hi,

I would like to add a suffix to a file name but maintain the suffix length to 5 digits.

For eg,

output > 1st_file.00001, 2nd_file.00002...10th_file.00010....100th_file.00100

Can anyone please advise me on how to go about it?

Platform: SunOS mps201a 5.9 Generic_118558-39 sun4u sparc SUNW,Sun-Fire-15000

Thanks

Danish

do you have seq command ?

seq -w 00001 00010

Hello Danish,

Assuming you are using ksh, then it is quite simple:-

typeset -Z5 counter=1
while [ some condition ]
do
   output > filename.$counter
   ((counter=$counter+1))          # Simple ksh 'add 1'
done

The typeset command states that this is a 5 character variable with leading zeros.

The code could be elaborated to generate 1st_file.00001, 2nd_file.00002 etc, but I haven't written that as I doubt it is actually necessary. If it is, just ask.

I hope that this helps,

Robin
Liverpool/Blackburn
UK

$ for i in *; do j=`expr $j + 1`; echo "$i" | nawk -v seq="$j" '{printf("%s %05i\n",$1,seq)}'; done > output.txt
$ while read name suffix; do echo mv $name $name.$suffix; done < output.txt 

Once, you are satisfied with the above echo command, you can remove the echo command.

Mostly SunOS wont support seq command .. If yes, try with the below ..

i=1; while [ $i -lt 10 ]; do OUTPUT > file.$(printf "%05d\n" $i) ; i=$((i+1)); done

I have just given the limit to 10 .. Change it to your need ..

Sorry itkamaraj..I don't have seq

rbatte..I tried with the following code..but its not giving the desired output..is not giving the leading zeros..

typset -Z5 counter=1
for i in `cat files`
do
   echo TT.${i}.${counter}
   ((counter=$counter+1))          # Simple ksh 'add 1'
done

<
TT.TTFILE_M1M1_201111210244365799.
TT.TTFILE_M1M1_201111210259375800.1
TT.TTFILE_M1M1_201111210422195807.2
TT.TTFILE_M1M1_201111210407185806.3
TT.TTFILE_M1M1_201111210337165804.4
TT.TTFILE_M1M1_201111210352175805.5
 >

~

---------- Post updated at 07:16 PM ---------- Previous update was at 07:04 PM ----------

Mr Moderator,I will definitely read how to use code tags, but please excuse me this time as I am in a bit of hurry. Next time, I'll make sure..

@jayan_jay.

Your code works perfect. However, I modified it a bit. Let me know your comments for any other modifications..

i=1
for j in `cat files`

do


#while [ $i -le 100 ]



        echo  TT.$j.$(printf "%05d\n" $i)
        i=$((i+1))

done

Danish,

If this is a copy & paste of your script, then there is a spelling mistake in the typeset command. This also assumes you are running in korn shell. It seems to work fine here, but if you are still stuck, do write back.

Robin

Perhaps an over simplified solution:

 ls | awk '{printf "%s.%05d\n", $0, i++}'