LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-29-2011, 03:06 PM   #1
enteptain
LQ Newbie
 
Registered: Aug 2011
Location: Louisville,Ky
Distribution: Ubuntu 10.04
Posts: 18

Rep: Reputation: Disabled
Wink adding letters to front and numbers to back of wordlist,bash perl or awk


i would love some help guys, using the following i need upper and lower case letters 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 |

thanks in advance guys! all tips appreciated!!!

example
aword0000
aword0001
aword0002
bword0000
bword0001
bword0002

im looking for all combinations from single word or word list

Last edited by enteptain; 10-30-2011 at 09:33 PM. Reason: fixing how the question appeared for more exact answer
 
Old 10-29-2011, 04:50 PM   #2
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
I'm going to assume that you want to do this:

Given a file that contains the following items

Code:
Foo
Bar
Baz
Fred
Barny
Wilma
Pebbles
Dino
You want to print out

Code:
0000FooA
0001BarB
0002BazC
0003Freda
0004Barnyb
0005Wilmac
0006PebblesA
0007DinoB
Notice that since we're using 'ABCabc' as the letters at the end, and we have 8 elements, we cycle back to A and B.

Here's my solution:

Code:
#! /usr/bin/perl
use warnings;
use strict;

my @numbers=( 0000 .. 9999 );
my @letters=( 'A' .. 'C', 'a' .. 'c' );
my $counter=0;

while( <> ) {
    my $numcounter = $counter % scalar @numbers;
    my $lettercounter = $counter % scalar @letters;
    chomp;
    printf "%04d%s%s\n", $numbers[$numcounter], $_, $letters[$lettercounter] ;
    $counter++;
}
This will work with files and input piped into stdin. Also, your use of 'while ($line = <>) ...' is better style than my code using $_. No big deal for quick scripts, but it makes a difference when the script gets big.

Last edited by bartonski; 10-29-2011 at 04:56 PM.
 
Old 10-29-2011, 05:20 PM   #3
enteptain
LQ Newbie
 
Registered: Aug 2011
Location: Louisville,Ky
Distribution: Ubuntu 10.04
Posts: 18

Original Poster
Rep: Reputation: Disabled
almost

Quote:
Originally Posted by bartonski View Post
I'm going to assume that you want to do this:

Given a file that contains the following items

Code:
Foo
Bar
Baz
Fred
Barny
Wilma
Pebbles
Dino
You want to print out

Code:
0000FooA
0001BarB
0002BazC
0003Freda
0004Barnyb
0005Wilmac
0006PebblesA
0007DinoB
Notice that since we're using 'ABCabc' as the letters at the end, and we have 8 elements, we cycle back to A and B.

Here's my solution:

Code:
#! /usr/bin/perl
use warnings;
use strict;

my @numbers=( 0000 .. 9999 );
my @letters=( 'A' .. 'C', 'a' .. 'c' );
my $counter=0;

while( <> ) {
    my $numcounter = $counter % scalar @numbers;
    my $lettercounter = $counter % scalar @letters;
    chomp;
    printf "%04d%s%s\n", $numbers[$numcounter], $_, $letters[$lettercounter] ;
    $counter++;
}
This will work with files and input piped into stdin. Also, your use of 'while ($line = <>) ...' is better style than my code using $_. No big deal for quick scripts, but it makes a difference when the script gets big.
close, more like
AFoo0000
BBar0001
CBaz0002

thank you very much for the extended script advice! that's the direction its heading in many parts lol. why the 'A' quotes around the A?
 
Old 10-30-2011, 01:20 AM   #4
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
Quote:
Originally Posted by enteptain View Post
close, more like
AFoo0000
BBar0001
CBaz0002

thank you very much for the extended script advice! that's the direction its heading in many parts lol. why the 'A' quotes around the A?
That's because I'm using the 'use strict' directive (and so should you ;-) ).

'use strict' enforces a number of syntax rules that have to do with how variables and constants are named and/or used.

The two things that it typically catches are variables which are not defined with the 'my' or 'our' keywords, and the use of 'barewords', i.e. words which are not variables or keywords which are not quoted.

If I had not put quotes around the string constants, I would have gotten the following compile errors:

Code:
Bareword "C" not allowed while "strict subs" in use at /tmp/test.pl line 6.
Bareword "A" not allowed while "strict subs" in use at /tmp/test.pl line 6.
Bareword "c" not allowed while "strict subs" in use at /tmp/test.pl line 6.
Bareword "a" not allowed while "strict subs" in use at /tmp/test.pl line 6.
Execution of /tmp/test.pl aborted due to compilation errors.
Using the 'strict' directive will save you all kinds of frustration from mis-spelled variables and having the perl interpret barewords in bizarre ways... and while you're at it, always use the warnings directive as well, and fix any warnings emitted. The other way leads to insanity.
 
Old 10-30-2011, 09:35 PM   #5
enteptain
LQ Newbie
 
Registered: Aug 2011
Location: Louisville,Ky
Distribution: Ubuntu 10.04
Posts: 18

Original Poster
Rep: Reputation: Disabled
love the script by the way! i re-edited how the question is asked to explain better what I'm looking for
 
  


Reply

Tags
awk, 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
[SOLVED] Help fixing bash/awk code to print letters based on counter Perseus Programming 37 10-28-2011 05:56 PM
[SOLVED] need help adding letters to beginning of words from list using perl,bash or awk enteptain Programming 2 08-24-2011 11:15 PM
wordlist/hex generator without repeating letters. jumpingj Programming 3 05-18-2011 07:44 AM
perl chop numbers off os a string - leave letters. casperdaghost Linux - Newbie 5 08-13-2010 11:59 PM
pass value from awk back to bash shell cjs_pro Programming 2 03-07-2005 04:53 PM


All times are GMT -5. The time now is 01:38 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