LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-01-2011, 07:37 PM   #1
enteptain
LQ Newbie
 
Registered: Aug 2011
Location: Louisville,Ky
Distribution: Ubuntu 10.04
Posts: 18

Rep: Reputation: Disabled
Talking 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
 
Old 11-02-2011, 06:57 AM   #2
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
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

Last edited by Juako; 11-02-2011 at 08:07 AM.
 
Old 11-02-2011, 08:10 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,561

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
@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.
 
Old 11-02-2011, 07:37 PM   #4
enteptain
LQ Newbie
 
Registered: Aug 2011
Location: Louisville,Ky
Distribution: Ubuntu 10.04
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
@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

Last edited by enteptain; 11-02-2011 at 07:49 PM. Reason: dual answer
 
  


Reply

Tags
bash, perl, perlscript, programing


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Design Front end Using Shell Script ?? shivarajM Linux - Software 3 04-27-2009 09:29 AM
GUI front-end for Perl script: opinions? trashbird1240 Programming 13 04-02-2007 09:39 AM
Recommendations for perl script front-end ojaiguy Linux - General 3 07-03-2006 06:34 AM
Recommendations for perl script front-end ojaiguy Linux - Software 1 07-03-2006 04:03 AM
putting word in front of each line in a file stryka Slackware 3 12-06-2005 02:32 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration