vr786 has asked for the wisdom of the Perl Monks concerning the following question:
Hi monks, I have written a perl script to comment the lines (where in it finds the correct word in a file , that particular line will be commented) . Here the script is commenting the lines but adding the new lines,how to avoid the adding new lines into a file
#!/usr/bin/perl -w use strict; use warnings; my $file_name = "test.txt"; my @ip; my $ip; @ip = (qw/test test1/); open(my $in, '<', $file_name) || die "cannot open $file_name for read + $!"; open(my $out, '>', "$file_name.tmp") || die "cannot open $file_name.tm +p for write $!"; while( <$in>) { foreach $ip(@ip) { print $out "#" if(/\b$ip\b/i && !/^#/); #only one # at front of lin +e print $out $_; } } close ($in); close ($out); # save copy of original file rename ($file_name,"$file_name.bak") || die "problem with rename $!"; # replace original with the modified version rename ("$file_name.tmp", $file_name) || die "problem with rename $!"; =for I/O files #= file test.txt before running program testing is requrired see/for/test test/data/istesting testing/data/test1 data/data/test2 #= file test.txt after running program testing is requrired testing is requrired #see/for/test see/for/test #test/data/istesting test/data/istesting #testing/data/test1 testing/data/test1 data/data/test2 data/data/test2 =cut
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem in Commenting lines in perl
by tilly (Archbishop) on Dec 28, 2010 at 05:56 UTC | |
by vr786 (Sexton) on Dec 28, 2010 at 06:39 UTC | |
|
Re: Problem in Commenting lines in perl
by NetWallah (Canon) on Dec 28, 2010 at 06:08 UTC | |
|
Re: Problem in Commenting lines in perl
by k_manimuthu (Monk) on Dec 28, 2010 at 06:35 UTC | |
by GrandFather (Saint) on Dec 28, 2010 at 06:53 UTC | |
by vr786 (Sexton) on Dec 28, 2010 at 06:59 UTC |