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
    The problem is that $_ has a newline in it. You can remove that with chomp, and then explicitly add in a newline where you want it to in your print.

      Thanks for your response, where can i add the chomp in script , can you explain me clearly.

Re: Problem in Commenting lines in perl
by NetWallah (Canon) on Dec 28, 2010 at 06:08 UTC
    Your inner loop:
    foreach $ip(@ip) { print $out "#" if(/\b$ip\b/i && !/^#/); #only one # at front of lin +e print $out $_; }
    Calls "print $out" once for each item in @ip, causing double lines. The simplest fix (untested) is early exit:
    foreach $ip(@ip) { last if /^#/; # ALready has a hash . Note - LAST exists this loop next unless /\b$ip\b/i; # No match - so try the next item print $out "#"; last; # Remember to exit loop after one successful match } print $_; # Print onloy once.
    When you are ready, take the next step and learn regex alternation, for a more efficient solution.

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Re: Problem in Commenting lines in perl
by k_manimuthu (Monk) on Dec 28, 2010 at 06:35 UTC

    It's having problem to the 'foreach loop'. It checks the key term and print the content twice. To avoid this kind of circumstance, you may use the ‘next’ statement when you match string found.

    while( <$in>) { foreach $ip(@ip) { print $out "#" and next if(/\b$ip\b/i && !/^#/); #only one # at fro +nt of line } print $out $_; }

      Except that prints each line once for each match word if none of the matches are found in a line, and in any case once for each mismatched word before a match is found.

      True laziness is hard work

      Thank you k_manimuthu, its working fine.