replacing text at the beginning of a file in perl and appending to that output

Discussion in 'Perl Misc' started by aixenv@yahoo.com, Jul 20, 2005.

  1. Guest

    i've been trying to figure out how to do this for about 30mins my perl
    is very rusty and i've never been very good at text manipulation

    basically here is the problem, i have a file that has on each line:

    name: $emailusername

    i need to from this long file (2.6mb) generate a list on each line of:

    $emailusername@$mydomain.com

    for example:

    take

    name: bobj
    name: bobjo

    to




    the original file had lots of other data in it

    the output looked like so

    name: bobj
    passwd: xxxxxxxxxencryptedxxxxxxxxx
    clear passwd: cleartextpasswd
    uid: 0
    gid: 0
    flags: 0
    gecos: bobj
    limits: No user limits set.
    dir: /home/vpopmail/domains/mydomain.com/0/5/bobj
    quota: 31457280S
    usage: 0%
    last auth: Sat Jul 16 03:05:39 2005
    last auth ip: 1.2.3.4

    i did the following command to generate the file i need parsed

    cat vpopmail-users.txt |grep 'name' > vpopmail-names

    i have tried various perl CLI commands to no avail

    some help would be appreciated, i apologize for my lack of
    understanding this concept

    thank you
     
    , Jul 20, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > i've been trying to figure out how to do this for about 30mins my perl
    > is very rusty and i've never been very good at text manipulation
    >
    > basically here is the problem, i have a file that has on each line:
    >
    > name: $emailusername
    >
    > i need to from this long file (2.6mb) generate a list on each line of:
    >
    > $emailusername@$mydomain.com
    >
    > for example:
    >
    > take
    >
    > name: bobj
    > name: bobjo
    >
    > to
    >
    >
    >
    >
    > the original file had lots of other data in it



    I'm not clear if you want
    1) a list of just the email addresses
    2) to change the file so that the name: line is replaced with the
    address
    3) to add the addresses underneath each name.

    1)
    perl -ln -e'print "$1\@mydomain.com" if /^name:\s+(.*)/' input.txt >
    output.txt

    2)
    perl -lpi -e's/^name:\s+(.*)/$1\@mydomain.com/;' input.txt

    3)
    perl -lni -e'print; print "$1\@mydomain.com" if /^name:\s+(.*)/'
    input.txt


    Read about the -l, -n, -p, and -i options in:
    perldoc perlrun

    > some help would be appreciated, i apologize for my lack of
    > understanding this concept


    Not having knowledge is not something ao apologize for. Refusing to
    learn the necessary knowledge once you've been pointed at it, is. So
    make sure you read that perldoc. :)

    Paul Lalli
     
    Paul Lalli, Jul 20, 2005
    #2
    1. Advertising

  3. Guest

    thanks very much im looking at perldoc perlrun atm thanks again
     
    , Jul 20, 2005
    #3
  4. <> wrote in message
    news:...

    [snip]

    > basically here is the problem, i have a file that has on each line:
    > name: $emailusername
    >
    > i need to from this long file (2.6mb) generate a list on each line of:
    > $emailusername@$mydomain.com


    [snip]

    > the original file had lots of other data in it
    > the output looked like so
    >
    > name: bobj
    > passwd: xxxxxxxxxencryptedxxxxxxxxx
    > clear passwd: cleartextpasswd
    > uid: 0
    > gid: 0
    > flags: 0
    > gecos: bobj
    > limits: No user limits set.
    > dir: /home/vpopmail/domains/mydomain.com/0/5/bobj
    > quota: 31457280S
    > usage: 0%
    > last auth: Sat Jul 16 03:05:39 2005
    > last auth ip: 1.2.3.4
    >
    > i did the following command to generate the file i need parsed
    > cat vpopmail-users.txt |grep 'name' > vpopmail-names


    I would write those commands (plus what else is needed to achieve your goal)
    as one Perl program (assuming that $mydomain is the 'mydomain.com' from the
    line dir: /home/vpopmail/domains/mydomain.com/0/5/bobj):

    use strict;
    use warnings;

    my $inpname = 'vpopmail-users.txt';
    my $outname = 'vpopmail-names';

    open (INPFILE,"<$inpname")
    or die "Error: can't open file '$inpname' for input, reason: $!";
    open (OUTFILE,">$outname")
    or die "Error: can't open file '$outname' for output, reason: $!";

    my ($emailname, $mydomain);

    while (<INPFILE>) {
    if (m!^name:\s*(\w+)!) {
    die "Error: email is duplicate"
    if defined($emailname);
    $emailname = $1;
    }
    if (m!^dir:\s*/home/vpopmail/domains/(.*?)/!) {
    die "Error: domain is duplicate"
    if defined($mydomain);
    $mydomain = $1;
    }
    if (m!^quota:!) {
    die "Error: email and/or domain is incomplete"
    unless defined($emailname and $mydomain);
    print OUTFILE "$emailname\@$mydomain\n";
    ($emailname, $mydomain) = (undef, undef);
    }
    }

    close OUTFILE;
    close INPFILE;

    --
    Klaus
     
    Klaus Eichner, Jul 20, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    395
  2. striker
    Replies:
    4
    Views:
    370
    Bengt Richter
    Nov 15, 2005
  3. Bruno
    Replies:
    3
    Views:
    352
    mlimber
    Jun 2, 2006
  4. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    281
    Chris Hohmann
    Apr 11, 2005
  5. Jesse B.
    Replies:
    9
    Views:
    236
    Jesse B.
    Mar 27, 2010
Loading...

Share This Page