search & replace from input file

J

julia

I have a input file and want to create an output file that contains
the converted digit we find in inputfile. Can you help me fix the
script ?

Inputfile
23456
3216,98
3452
40
4
outputfile
*****
****,**
****
**
*

sub doReplace
{
my @outLines; #Data we are going to output
my $line; #Data we are reading line by line

open (FILE, $in ) or
die "Cannot open file: $!";

while ( $line = <FILE> )
{
$line =~ s/\d{4}/****/;
$line =~ s/\d{5}/*****/;
$line =~ s/\d{8}/*********/;

push(@outLines, $line);
}
close FILE;

open ( OUTFILE, ">$out" ) or
die "Cannot open file: $!";

print ( OUTFILE @outLines );
close ( OUTFILE );

undef( @outLines );
}
 
G

Glenn Jackman

At 2005-04-19 03:38PM said:
while ( $line = <FILE> )
{
$line =~ s/\d{4}/****/;
$line =~ s/\d{5}/*****/;
$line =~ s/\d{8}/*********/;

Think about what happens when $line = '12345678' after the first s///
You want to change the order:
$line =~ s/\d{8}/*********/;
$line =~ s/\d{5}/*****/;
$line =~ s/\d{4}/****/;

Why not s/\d/*/g ?
 
G

Gunnar Hjalmarsson

julia said:
I have a input file and want to create an output file that contains
the converted digit we find in inputfile. Can you help me fix the
script ?

Inputfile
23456
3216,98
3452
40
4
outputfile
*****
****,**
****
**
*

open my $IN, '<', $in or die $!;
open my $OUT, '>', $out or die $!;
while (<$IN>) {
tr/0-9/*/;
print $OUT;
}
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top