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

A

aixenv

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

(e-mail address removed)
(e-mail address removed)

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
 
P

Paul Lalli

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

(e-mail address removed)
(e-mail address removed)

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
 
K

Klaus Eichner

[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;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top