Re: [NEWBIE] newline question

From: Richard Morse (remorse_at_partners.org)
Date: 03/31/04


Date: Wed, 31 Mar 2004 12:40:12 -0500

In article <c4eupc$mqe$1@ariadne.rz.tu-clausthal.de>,
 "Jan Biel" <jan.biel@tu-clausthal.de> wrote:

> -------------------------------
> $filein = 'a.txt';
> $fileout = 'b.txt';
>
> open(INFO, $filein);
> open(INFO2, ">$fileout");
>
> @lines = <INFO>;

@lines = ( 'a\n', 'b\n', 'c\n' );

> grep(s/\n//g,@lines);

@lines = ( 'a', 'b', 'c');

> grep(s/ab/found/g,@lines);

At this point, no entry in @lines matched 'ab', so the substitute never
occurs.

Try this:

#!/usr/bin/perl

# always use these next two lines
use strict;
use warnings;

my $filein = 'a.txt';
my $fileout = 'b.txt';

open(my $in, "<", $filein) or die("Can't open $filein: $!");

# slurp all of the data into one string, since you really don't
# care about newline separations
my $data;
{
   local $/;
   $data = <$in>;
}
close($in);

# remove any newline characters
$data =~ s/\n//g;

# change 'ab' to 'found'
$data =~ s/ab/found/g;

# save the data
open(my $out, ">", $fileout) or die("Couldn't open >$fileout: $!");
print $out $data, "\n";
close($out);

__END__

HTH,
Ricky

-- 
Pukku