LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   adding letters to front and numbers to back of wordlist,bash perl or awk (http://www.linuxquestions.org/questions/programming-9/adding-letters-to-front-and-numbers-to-back-of-wordlist-bash-perl-or-awk-910843/)

enteptain 10-29-2011 03:06 PM

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

bartonski 10-29-2011 04:50 PM

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.

enteptain 10-29-2011 05:20 PM

almost
 
Quote:

Originally Posted by bartonski (Post 4511463)
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?

bartonski 10-30-2011 01:20 AM

Quote:

Originally Posted by enteptain (Post 4511481)
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.

enteptain 10-30-2011 09:35 PM

love the script by the way! i re-edited how the question is asked to explain better what I'm looking for


All times are GMT -5. The time now is 04:49 PM.