Incrementing with a twist - please help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Incrementing with a twist - please help
# 1  
Old 03-03-2010
Incrementing with a twist - please help

I'm currently trying to write a ksh or csh script that would change the name of a file found in directories and attach to the name an incrementing three digit number.
I know how to write a script that will go:
000, 001, 002, 003, etc

The twist is I need more increments then allowed by a 3 digit number so I want to use letters too. For example:
008, 009, 00A, 00B, 00C --- 00Z, 010, 011, 012, 013 --- 099, 0AA, 0AB --- 0ZZ, 100, 101, etc

Any slick ways of doing this? I searched for it but couldn't find anything like this on the forum.

Thanks!

Rust
# 2  
Old 03-03-2010
Code:
digits='0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z'
for i in $(eval echo {$digits}{$digits}{$digits}); do echo $i; done



---------- Post updated at 02:34 AM ---------- Previous update was at 02:25 AM ----------

By the way, your example sequence is inconsistent. 09A and 09B should follow 099, not 0AA and 0AB. Smilie

Regards,
Alister
# 3  
Old 03-03-2010
Code:
#!/usr/bin/perl

for $i  ( 000 .. 999 ) {
  if ($i < 10 ) {
    $int="00".$i
}
 elsif (( $i >= 10 ) && ( $i < 100 )){
     $int="0".$i
    }
 else {
  $int=$i;
   }

  for $j  ( A .. Z ){
  print "$int$j\n"
}
}

cheers,
Devaraj Takhellambam
# 4  
Old 03-03-2010
Thanks for you help guys. And your right Alister, I was off on my example. Smilie

Going to test it now.
# 5  
Old 03-03-2010
alister's script is best. Can you explain why "eval" can generate the data automatically?

Code:
$ eval echo {$digits}{$digits}{$digits} |more
000 001 002 003 004 005 006 007 008 009 00A 00B 00C 00D 00E 00F 00G 00H 00I 00J 00K 00L 00M 00N 00O 00P
....

Here is my code.
Code:
digits='0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'
for i in ${digits}
do
  for j in $digits
  do
    for k in $digits
    do
       echo "$i$j$k"
    done
  done
done

# 6  
Old 03-04-2010
Hi, rdcwayx

It's not the eval which generates the data; it's brace expansion.

Code:
#Non-numeric example
$ echo f{ee,i,o,um}
fee fi fo fum

#Numeric example
$ echo {0,1}{0,1}{0,1}
000 001 010 011 100 101 110 111

Since the original poster's problem requires a fairly long sequence of digits, for brevity I wanted to avoid:
Code:
echo {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z}{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,etc.....

Storing the list in the variable $digits allows me to do that. However, since brace expansion occurs before variable expansion, we need to send the command line through the shell's parser a second time, using eval. Otherwise we just get:
Code:
$ digits=0,1
$ echo {$digits}{$digits}{$digits}
{0,1}{0,1}{0,1}

Instead of:
Code:
$ eval echo {$digits}{$digits}{$digits}
000 001 010 011 100 101 110 111

Note: Brace expansion does not occur if there is no unquoted comma within the braces, so we do not need to quote {$digits}.

A downside to this approach is that if the list of digits is long enough, or if there are many braced terms to expand, the list of resulting words could exceed the command line length limit. In such a case, your approach is superior as it never needs to execute a long command line. Another way around this limitation would be to write it out explicitly in the for loop:
Code:
$ for i in {0,1}{0,1}{0,1}; do echo $i; done
000
001
010
011
100
101
110
111

IMPORTANT: Brace expansion is not a posix-standardized feature, so if that's a concern, my approach is not an option; yours would be the better choice. Also, the order of brace expansion in relation to parameter expansion as described above is BASH specific (which I chose to use for the illustrative examples since it's much more popular than ksh, and because for this particular scenario it's the more complicated case). In pdksh, ksh88, and ksh93, brace expansion occurs after parameter expansion, not before. So, if using a ksh variant, neither the eval nor the echo nor the command substition is necessary (though in this case their presence does not affect the end result); a simple 'for i in {$digits}{$digits}{$digits}' would be sufficient.

Hope that helps. If not, feel free to ask for clarification.

Cheers,
Alister

Last edited by alister; 03-04-2010 at 09:44 AM..
# 7  
Old 03-04-2010
Thank you so much for the explanation.

One question for Moderators,

I try to give bits awards to alister's reply, but my ID can't do that now.

I remember I can give before. Can you help?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File Listing, with a Twist?

Greetings! I have a quick question which must be deferred to those with greater skill than myself :) In this situation, I wish to create a list of all the files on an entire partition in descending order sorted by date. I tried numerous switches for ls, and found this line to be the closest... (4 Replies)
Discussion started by: LinQ
4 Replies

2. Shell Programming and Scripting

Simple two file compare with twist

I have file1 and file2 I lookup field3 from file2 in field1 of file1 and if there is a match, output field 2,3,5 from file2. I now want to add field2 of file1 in the output. I suspect what I have to do is read the entire line of file1 into a 2 dim array? pls help. (1 Reply)
Discussion started by: tmonk1
1 Replies

3. Shell Programming and Scripting

Section Removal With sed; and With a Twist . . .

Hello folks! Raised a bump on my head trying to figure this one out ;) I have an xml file which needs to be edited, removing an entire property section in the work. Here's what the target section layout looks like: <property name="something"> {any number of lines go here} </property>... (7 Replies)
Discussion started by: LinQ
7 Replies

4. Shell Programming and Scripting

Simple two file compare with twist

I have file1 and file2 I lookup field3 from file2 in field1 of file1 and if there is a match, output field 2,3,5 from file2. I now want to add field2 of file1 in the output. I suspect what I have to do is read the entire line of file1 into a 2 dim array? pls help. here is my code: ... (9 Replies)
Discussion started by: jack.bauer
9 Replies

5. Shell Programming and Scripting

Delete duplicate lines... with a twist!

Hi, I'm sorry I'm no coder so I came here, counting on your free time and good will to beg for spoonfeeding some good code. I'll try to be quick and concise! Got file with 50k lines like this: "Heh, heh. Those darn ninjas. They're _____."*wacky The "canebrake", "timber" & "pygmy" are types... (7 Replies)
Discussion started by: shadowww
7 Replies

6. Shell Programming and Scripting

How to merge two files with a slight twist

Hi, a brief introduction on the soundex python module(english sound comparison): import soundex.py a = "neu yorkk" b = "new york city" print soundex.sound_similar(a, b) output: 1 Suppose I want to merge two files, called mergeleft.csv and mergeright.csv Mergeleft.csv: ... (0 Replies)
Discussion started by: grossgermany
0 Replies

7. UNIX for Dummies Questions & Answers

file count with a twist

Hello Everyone, I am using the korn shell. I was hoping to find a set of commands to count files in a directory. I am using: ls /home/name/abc* | wc -l This command works fine when a file matches abc* (returns only the file count) , however when no file(s) are found I get... (2 Replies)
Discussion started by: robert4732
2 Replies

8. UNIX for Advanced & Expert Users

building a kernel (with a twist)

Hey all, I am working on a static analysis tool and I wan't to see if it can find bugs in the linux kernel, it uses LLVM framework to analyse the instructions. Long story short I need to build the kernel with a custom compiler. The compiler will create byte code files where binaries usually... (2 Replies)
Discussion started by: zigga15
2 Replies

9. Shell Programming and Scripting

Compare 2 files yet again but with a twist

Ok so I have a file which contains 2 columns/fields and I have another file with 2 columns. The files look like: file1: 1 33 5 345 18 2 45 1 78 31 file2: 1 c1d2t0 2 c1d3t0 3 c1d4t0 4 c1d4t0 5 c2d1t0 6 c2d1t0 7 c2d1t0 8 c2d1t0 9 c2d1t0 10 c2d1t0 (11 Replies)
Discussion started by: Autumn Tree
11 Replies

10. UNIX for Dummies Questions & Answers

how do I log into this machine - with a twist...

I know this topic has been covered in one form or another, but it hasn't been covered to handle my problem. I was given a Sparc4 running Solaris 2.5.1 The root password is unknown. This machine has no cdrom drive and it has no floppy drive. I tried booting into the single user mode, but... (1 Reply)
Discussion started by: xyyz
1 Replies
Login or Register to Ask a Question