in reply to Error using grep
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.
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).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Error using grep
by justkar4u (Novice) on Mar 29, 2011 at 02:16 UTC | |
by GrandFather (Saint) on Mar 29, 2011 at 03:09 UTC | |
by justkar4u (Novice) on Mar 29, 2011 at 18:13 UTC | |
by GrandFather (Saint) on Mar 29, 2011 at 20:06 UTC | |
by justkar4u (Novice) on Mar 29, 2011 at 23:00 UTC |