The following sample may or may not fix your immediate problem (you've not given enough information for me to be able to tell), but it will avoid a whole slew of problems in the future:

#!/usr/bin/perl use strict; use warnings; print "Enter the filename to be parsed\n"; my $fileName = <STDIN>; print "Enter the keyword to be searched in the file\n"; my $keyword = <STDIN>; chomp $fileName; chomp $keyword; open my $inFile, '<', $fileName or die "Failed to open $fileName: $!\n +"; # Search for the key pattern in the file information. while (defined (my $line = <$inFile>)) { chomp $line; next if $line ne $keyword; print "Found key word $keyword on line $.\n"; exit; } print "Keyword not found\n";

Always use strictures (use strict; use warnings;).

Always use three parameter open, lexical file handles (using my) and check the result from open.

Use chomp not (ever) chop.

Don't slurp files into an array - use a while loop instead.

Note that this may no work for you because it expects to match the entire line against the key word. If you just want to see if the line contains the key word then use the regular expression $line =~ /\Q$keyword\E/ or the expression 0 <= index $line, $keyword (although there are problems with both of these test too, depending on what you actually want to match).

True laziness is hard work

In reply to Re: Error using grep by GrandFather
in thread Error using grep by justkar4u

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.