Lonewolf wrote:
>
> I have a file that I need to replace the *| and || with just | and can not
> seem to get it to go. It either puts in an | every space, or it puts in
> multiples of them, how do I fix this?
>
> here's what I have:
> sub cleanup{
>
> use strict;
>
> my $file = "/home/web/sales/info/test/custs1";
> my $newfile = "/home/web/sales/info/test/fixed.txt";
> my $line;
>
> open (OLDFILE, "< $file");
> open (NEWFILE, "> $newfile");
You should *ALWAYS* verify that the files were actually opened.
> while ($line = <OLDFILE>) {
You should declare $line here to limit its scope.
> $line =~ s/||/|/mg;
> $line =~ s/\*|/|/mg;
The '|' character is special in a regular expression. /||/ means "match
'' or '' or ''". You have to escape special characters to match the
literal character. The /m option is used with the line anchors ^ and $
but since you are not using them it is not required.
> print NEWFILE "$line\n";
> }
> close OLDFILE;
> close NEWFILE;
>
> print "$newfile has now been created\n";
> }
use warnings;
use strict;
my $dir = '/home/web/sales/info/test';
my $file = "$dir/custs1";
my $newfile = "$dir/fixed.txt";
open OLDFILE, "< $file"; or die "Cannot open $file: $!"
open NEWFILE, "> $newfile" or die "Cannot open $newfile: $!";
while ( my $line = <OLDFILE> ) {
# use a character class to specify either '*' or '|'
# character classes don't require '*' or '|' to be escaped
$line =~ s/[*|]\|/|/g;
print NEWFILE "$line\n";
}
close OLDFILE;
close NEWFILE;
print "$newfile has now been created\n";
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>