opensourcer has asked for the wisdom of the Perl Monks concerning the following question:

hi monks,
i kow few of them won't like this code because there may be a shorter version of this code, and i want the shorter version of this code.
#!/usr/bin/perl -w #opening Rules1.txt for reading open(FILE, "<Rules1.txt") or die "unable to open Rules1.txt: err at $! +"; #copying everything to array #what if it's huge file # i don't what to do @array = <FILE>; #closing the file handle close(FILE); #im looping so that i can replace with what im looking for foreach $line (@array) { $line =~ s#<b>#/*<b>#ig; $line =~ s#</b>#</b>*/#ig; } #again open for writing open(FILE, ">Rules1.txt") or die "unable to open Rules1.txt: err at $! +"; #write to FILE print FILE @array; #close FILE close (FILE);
That's it
"I really don't live on earth, it's just my reflection"
"open source is not only technology, but it's a world out there - opensourcer"

Replies are listed 'Best First'.
Re: replaceing text in file using replace prattern
by Thilosophy (Curate) on Feb 07, 2005 at 09:16 UTC
    and i want the shorter version of this code.

    If I read your code correctly, all you want to do is run two regex substitutions on the input file. This does not even have to be a Perl script, it could be a Perl command-one-liner:

    perl -pe 's#<b>#/*<b>#ig; s#</b>#</b>*/#ig;' -i Rules1.txt

    Check out this good tutorial on perl.com about file editing.

Re: replaceing text in file using replace prattern
by pingo (Hermit) on Feb 07, 2005 at 10:16 UTC
    You could also try using sed:
    sed -e "s#<b>#/*<b>#ig" -e "s#</b>#</b>*/#ig" Rules1.txt > tmp.txt
Re: replaceing text in file using replace prattern
by gube (Parson) on Feb 07, 2005 at 09:45 UTC

    Hai, Try this simple

    undef $/; open(IN, "D:\\temp.log"); $str = <IN>; print $str; close(IN); $str =~ s#<b>#*<b>#gsi; $str =~ s#</b>#</b>*#gsi; open(OUT, "> D:\\temp.log"); print OUT $str; close(OUT);

    Regards,
    Gubendran.L
Re: replaceing text in file using replace prattern
by kprasanna_79 (Hermit) on Feb 07, 2005 at 13:48 UTC
    #!/usr/bin/perl -w #opening Rules1.txt for reading open(FIN, "< Rules1.txt") or die "unable to open Rules1.txt: err at $! +"; #opening for writing open(FOUT, "> Rules.txt") or die "unable to open Rules1.txt: err at $! +"; while(<FIN>) { $line = $_; $line =~ s#<b>#/*<b>#ig; $line =~ s#</b>#</b>*/#ig; print FOUT $line; } close(FOUT);