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
In reply to Problem in Commenting lines in perl by vr786
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |