Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > replacing text at the beginning of a file in perl and appending to that output

Reply
Thread Tools

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

 
 
aixenv@yahoo.com
Guest
Posts: n/a
 
      07-20-2005
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

http://www.velocityreviews.com/forums/(E-Mail Removed)
(E-Mail 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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-20-2005
(E-Mail Removed) 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
>
> (E-Mail Removed)
> (E-Mail 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

 
Reply With Quote
 
 
 
 
aixenv@yahoo.com
Guest
Posts: n/a
 
      07-20-2005
thanks very much im looking at perldoc perlrun atm thanks again

 
Reply With Quote
 
Klaus Eichner
Guest
Posts: n/a
 
      07-20-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...

[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


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
index of string from beginning of line vs beginning of file Jesse B. Ruby 9 03-27-2010 04:04 PM
"Appending" at beginning of file Bruno C++ 3 06-02-2006 05:00 PM
replacing multiple instances of commas beginning at specific position striker Python 4 11-15-2005 11:49 PM
Dynamic dropdows appending not replacing second dropdown items seanmayhew@gmail.com ASP .Net 1 04-16-2005 04:04 PM



Advertisments