PDA

View Full Version : Hard Return


progressweb
08-09-2002, 07:49 PM
I have a flat text file with a list of email addresses each with a hard return after the address. Is there a way to replace the hard return with a ; ?? Thanks in advance!

Mouldy_Goat
08-10-2002, 01:20 AM
Something like this perhaps:
#!/usr/bin/perl -w
use strict;

my $filename, $data;
$filename = "dbfile.txt";

open (DATA, "<$filename") || die "Couldn't open $filename for reading: $!";
undef $/; #this means the next line will slurp all the data in..
$data = <DATA>;
$/ = "\n";
close DATA;

$data =~ s/\n/;\n/sg;

open (DATA, ">$filename") || die "Couldn't open $filename for writing: $!";
print DATA $data;
close DATA;

That's how I'd do it... hope it helps a bit.

pager
08-14-2002, 05:59 AM
If he wanted to actually REPLACE the hard CR with a semicolon...

$data =~ s/\n/;/sg;

Which should give the string instead:

joe@noemail.com;george@yyahoo.com;clumsy@dummy.com

then open write print etc..

However, I've run across this problem with some servers...
If the file has "@" directly in it, then that needs to be escaped as well.

Maybe add to the substitution list:
$data=~ s/\@/\\@/sg;

It all depends on the server/perl config, and the program that's working with it though. I've had problems with the stupid @ character in parsed files before, and I use 3 different sun/unix/linux setups... well heck what do I know. They got idiot brains of their own.

Good grief, these escaped slashes make me mad anyways. I mean, is there a REAL exact list of characters and combinations that MUST be escaped so's Perl never produces an error? 100 percent of the time? I wish that linux/unix admins followed all of the same standard protocols for their server setups.

It ain't the 95% that's the problem, it's the 5%.

hmmm, maybe the single semi-colon sub needs to be escaped also? dunno. haven't tried it.