LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   need help with script putting 1 letter in front of a word and 4 numbers on the end (http://www.linuxquestions.org/questions/programming-9/need-help-with-script-putting-1-letter-in-front-of-a-word-and-4-numbers-on-the-end-911367/)

enteptain 11-01-2011 07:37 PM

need help with script putting 1 letter in front of a word and 4 numbers on the end
 
i would love some help guys, using the following i need upper or lower case letter in the front of the word and 0000-9999 at the end. i did experiment a little but just keep getting errors

perl -e 'while(my $line = <>) { chomp($line); foreach my $i (0000..9999) {printf ("$line%04d\n", $i); }}' pw.txt >> pw2.txt

perl -e 'while(my $line = <>) { chomp($line); foreach my $i (A..C,a..c) {printf ("%s$line\n", $i); }}' pw.txt >> pw2.txt

i'm also working of making it feed into other programs with | ....so if this can be a one liner it would be great

thanks in advance guys! all tips appreciated!!!

example
aword0000
aword0001
aword0002
bword0000
bword0001
bword0002
cword0000 etc

im looking for all combinations from single word or word list. perl preferred but obviously not required. ALL advice welcome!!! i'm really loving learning even little scripts. i haven't gone far into awk yet

Juako 11-02-2011 06:57 AM

In bash I'd do it like this:

Code:

#!/bin/bash
letters=({{a..z},{A..Z}})
declare -i letter=0 number=0

while read line; do
    echo ${letters[$letter]}$line$(printf %04d $number)
    (( ++letter == ${#letters[@]} )) && letter=0
    (( ++number == 10000 )) && number=0
done

The script populates an array $letters (using brace expansion) and declares two indices: $letter (for indexing the array) and $number (for the number counter).

Then it iterates on each line of standard input echoing a string composed of: an entry of $letters indexed by $letter, followed by the $line, followed by $number (uses a command substitution to call printf, which allows for zero padding).

Finally, the last two arithmetic expressions reset $letter if it's higher than the last index of $letters, and $number if it's higher than 9999.

You can test it easily with something like:
Code:

yes | sed 's/./this is a line/' | ./scriptname
Additionaly, you can trim it some more at the expense of readability by using just one index (0 to 99999). Where you want to use it for indexing the array, use a modulus (remainder) operation to adjust it. This will also gain you some tiny extra cpu cycles:
Code:

#!/bin/bash
letters=({{a..z},{A..Z}})
declare -i index=0

while read line; do
    echo ${letters[$((index % ${#letters[@]}))]}$line$(printf %04d $index)
    (( ++index == 10000 )) && index=0
done


grail 11-02-2011 08:10 AM

@OP - So did you not like the answers or the direction this question was going in when you asked it previously here? It is taken as rude to abandon a question only to ask the exact same thing again.

enteptain 11-02-2011 07:37 PM

Quote:

Originally Posted by grail (Post 4514081)
@OP - So did you not like the answers or the direction this question was going in when you asked it previously here? It is taken as rude to abandon a question only to ask the exact same thing again.

i loved the answer!!!! i didnt think the question was asked correctly and wanted it to be easier to find if some one else looked for what i was asking.
i'm testing it now then i'll mark answered asap. just got home from work. i seen the answer at work but couldn't take a break lol


All times are GMT -5. The time now is 11:56 PM.