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

I have never posted a question here. I would really appreciate if any one can answer this. Please let me know if the question is not clear

I have 2 files which are file1 and file2. I need to search for each of the lines from file1 in file2. And if it exists then comment that line in file2. The line thats being searched will be just part of the line in file2.

Text in file1:

Text in file2:
I am in perlmonks
I am posting question

output file which is again file2 should look like this:
#I am in perlmonks
I am posting question
  • Comment on how to comment the lines if found a match

Replies are listed 'Best First'.
Re: how to comment the lines if found a match
by lostjimmy (Chaplain) on Dec 05, 2008 at 18:55 UTC
    Since you haven't provided any code, how about some pseudocode to get you started?
    OPEN file1 READ contents of file1 into array @words OPEN file2 FOR line in file 2 IF line contains any of @words PRINT "#" + line ELSE PRINT line
    You might want to check out the documentation for open and perlretut.
Re: how to comment the lines if found a match
by moritz (Cardinal) on Dec 05, 2008 at 18:53 UTC
    So what have you tried so far?

    My idea: read the first file, build a regex from all lines (by joining the lines with |), then read the second file, and prepend a # to all lines which match the regex.

    (It's easier if you first produce the correct output instead of editing the file in-place).

Re: how to comment the lines if found a match
by ww (Archbishop) on Dec 05, 2008 at 20:55 UTC
    Your explanation is adequately clear.

    Your answer can be found by using Search (above) or Super Search, as your question is a minor variation on one that comes up again and again.

      This is how my program looks like now. I tried this but the grep here is not returning right value, its returning 1 as if its matching all the time.

      #!/usr/bin/perl -w
      open(MATCH_PATTERN,"< temp1");
      open(FILE_TO_LOOK,"< temp2");
      open OF, "> temp3";

      while ($m =<MATCH_PATTERN>)
      print "reading the current text to match \n $m \n";
      seek (FILE_TO_LOOK ,0, 0);
      while ($l= <FILE_TO_LOOK>) {
      print "reading the textline to look for match \n $l \n";
      @found = grep ($m, $l) ;
      $match = @found;
      if ($match == 1)
      print "now the match is \n $match \n";
      print OF "#$l";

      close FILE_TO_LOOK;
      close MATCH_PATTERN;
      close OF;

        You are misunderstanding the use of grep.

        In your example, grep will always return $l unless $m happens to be 0 or an empty string.

        Instead of grep, you probably want a regular expression.

        if($l =~ /\Q$m\E/) { print "We matched\n"; print OF "$$l"; }

        You probably also want to use chomp in the outer loop to remove the line ending from $m.

        You are also writing the file multiple times with different lines commented each time. You might want to generate a list of the expressions to match at the beginning and then loop over the file once.

        G. Wade
Re: how to comment the lines if found a match
by n3toy (Hermit) on Dec 06, 2008 at 00:18 UTC
    There are a few shortcuts that could make this smaller, but the code below is one way to comment lines that matched a list of words.

    However, you will probably need to dial in the regex to better meet your needs.

    #!/usr/bin/perl use strict; use warnings; my @file_a_array; my $newfile; my $file_a = <path to word file>; my $file_b = <path to lines of text>; open (FILE_A, "$file_a") or die "couldn't open the file!"; while (my $line = <FILE_A>) { chomp ($line); push (@file_a_array, $line); } close (FILE_A); open (FILE_B, "$file_b") or die "couldn't open the file!"; while (my $line = <FILE_B>) { chomp ($line); foreach my $word (@file_a_array) { if ($line =~ /$word/) { $newfile = $newfile . "# "; } } $newfile = $newfile . $line . "\n"; } close (FILE_B); open (FILE_B, ">$file_b") or die "couldn't open the file!"; print FILE_B $newfile; close (FILE_B); print "end\n";

    Hope this helps!

      Would you mind, if I suggest very small suggestion to your code?

      $! will give more information regarding the type of failure

      open (FILE_A, "$file_a") or die "couldn't open the file!"; >>open (FILE_A, "$file_a") or die "couldn't open the file $file_a <$!> +!";
      close function will also fail, its better to check that as well

      close (FILE_A); >>close (FILE_A) or die "Unable to close file";
        Thanks alot. This works exactly how I want. Thanks for all your support.