increment counter as suffix starting with the rightmost digit


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting increment counter as suffix starting with the rightmost digit
# 1  
Old 12-21-2011
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
# 2  
Old 12-21-2011
do you have seq command ?

Code:
seq -w 00001 00010

# 3  
Old 12-21-2011
Hello Danish,

Assuming you are using ksh, then it is quite simple:-
Code:
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
# 4  
Old 12-21-2011
Code:
$ 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.
# 5  
Old 12-21-2011
Mostly SunOS wont support seq command .. If yes, try with the below ..
Code:
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 ..
# 6  
Old 12-21-2011
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..
Code:
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
 >

~


Moderator's Comments:
Mod Comment How to use code tags


---------- 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..
Code:
i=1
for j in `cat files`

do


#while [ $i -le 100 ]



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

done


Last edited by Franklin52; 12-21-2011 at 09:48 AM.. Reason: Please use code tags for code and data samples, thank you
# 7  
Old 12-22-2011
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Process only 4 digit odd number starting with zero

I am trying to process only IonCode_odd #'s (always 4 digits starting with zero), but the below isn't working as expected. Is there a better way? Thank you :). IonCode_0401_xxxx_xxxx_xxxx.bam IonCode_0401_xxxx_xxxx_xxxx.bam.bai IonCode_0401_xxxx_xxxx_xxxx.fastq... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk new line rightmost

Hi All i have a file say test with the following output vserver share-name path acl test test test "test/Read", "test/write" Expecting the below output ... (5 Replies)
Discussion started by: venkitesh
5 Replies

3. Shell Programming and Scripting

Bash counter increment not working

Hi all, I'm using Bash 4.3.8 on an Ubuntu system, and no matter what I try, incrementing a counter won't work. The simplest example would be something like this: #!/bin/bash myVar=0 myVar=$((myVar++)) echo myVar The variable should be 1, but it's always 0. I've tried every increment... (6 Replies)
Discussion started by: Zel2008
6 Replies

4. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

5. Shell Programming and Scripting

Change filename - increment digit by 60

I have a bunch of files, they have spaces! I am working in windows 7, but I am hoping I could install and use cygwin or some other unix platform to rename my files (suggestions welcome). The files are named: Wind Attn Vol - 474.wrl Wind Attn Vol - 475.wrl Wind Attn Vol - 476.wrl etc.. I... (10 Replies)
Discussion started by: d_sai_kumar
10 Replies

6. Shell Programming and Scripting

convert two digit in to single digit...

Hi Guys. My Input: ABCD 12 00 KL ABCD 12 08 DL ABCD 12 10 KK ABCD 12 04 LL ABCD 13 00 LP ABCD 13 1O LS Output: ABCD 12 0 KL ABCD 12 8 DL ABCD 12 10 KK ABCD 12 4 LL ABCD 13 0 LP (2 Replies)
Discussion started by: pareshkp
2 Replies

7. Shell Programming and Scripting

awk length of digit and print at most right digit

Have columns with digits and strings like: input.txt 3840 3841 3842 Dav Thun Tax Cahn 146; Dav. 3855 3853 3861 3862 Dav Thun Tax 2780 Karl VI., 3873 3872 3872 Dav Thun Tax 3894 3893 3897 3899 Dav Thun Tax 403; Thun 282. 3958 3959 3960 Dav Thun Tax 3972 3972 3972 3975 Dav Thun Tax... (8 Replies)
Discussion started by: sdf
8 Replies

8. UNIX for Dummies Questions & Answers

bash script to increment a digit in filename

Hi guys, Can someone help me out with this: I have a directory with files like the following, GHost++ 2010-03-14 04-01 DotaCash RD us_ca LC #7 (44m19s).w3g GHost++ 2010-03-14 04-06 DotaCash AP us_ca LC #8 (42m24s).w3g GHost++ 2010-03-14 04-07 DotaCash AR us_ca LC #10 (08m23s).w3g ... (4 Replies)
Discussion started by: hbjlee17
4 Replies

9. Shell Programming and Scripting

Increment counter in ksh

Hello, I am making an increment counter in ksh. So i write a number in a temporary file and when I execute my script I read this file and I make +1. But how can I make this with a number with 6 digits , example 000009 + 1 = 000010 ... .... .... 000099 + 1 = 000100 Do anyone know... (5 Replies)
Discussion started by: steiner
5 Replies

10. UNIX for Dummies Questions & Answers

counter / increment problem within echo stmt

Simple script trying to increment a counter within an echo statement never gets past 1 - PLEASE HELP! Thanks. ~~~~~~~~~~~ #!/bin/sh stepup() { STEP=`expr $STEP + 1` echo $STEP } # # Initialize variables # STEP=0 echo "Counter Value: `stepup`" echo "Counter Value:... (2 Replies)
Discussion started by: blaze
2 Replies
Login or Register to Ask a Question