in reply to Re^3: Error using grep
in thread Error using grep
-----------------------------------------file------------- This file is already created by some other script. Now I want to search for a key say "xyz" in this case and print the ENTIRE LINE containing the key and its status etc. The above code or my previous code works fine except that it also prints the "keyword not found" msg even after finding and printing the keyword and its line!! How do I get rid of that. I tried using else statement but it prints the "key not found" msg for all the other lines(i mean multiple times).#!/usr/bin/perl # main program; use strict; use warnings; print "Enter the filename to be parsed\n"; my $file = <STDIN>; open (DATA,$file); print "Enter the keyword to be searched in the file\n"; chomp(my $key = <STDIN>); # Search for the key pattern in the file information. while (defined (my $line = <DATA>)) { next if $line !~ /\b\Q$key\E\b/i; print "$line\n"; } print "Keyword not found\n";
Also I am doing this to give the user more flexibility in searching for any key in any input file and print the entire line not line number for his/her reference..Thanks for your help in advance . - RamController Version Information -------------------------------------------------------- BIOS : 0.2-0 (18298) Firmware : 0.2-0 (18298) Driver : 0.1-7 (28000) Boot Flash : 0.2-0 (18298) -------------------------------------------------------- Controller xyz Information -------------------------------------------------------- Status : xyz Optimal
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Error using grep
by GrandFather (Saint) on Mar 29, 2011 at 20:06 UTC | |
by justkar4u (Novice) on Mar 29, 2011 at 23:00 UTC |