in reply to Re: Error using grep
in thread Error using grep

Hi, Sorry for not being clear earlier. I do want to search for any keyword(mostly alphabetical words) and if present in that line , I need to display the entire line.Line may have spaces or special characters. I do not know how to use the boundary conditions. I tried using {/\b\Q$key\E\b/} or outside the {\b/\Q$key\E/\b} but either of them dint work. I also read about using /p to preserve the prematch ,match and post match expressions but I got this error :Illegal division by zero at ./test1.pl line 14, <STDIN> line 2. for entering zmm keyword.

Replies are listed 'Best First'.
Re^3: Error using grep
by GrandFather (Saint) on Mar 29, 2011 at 03:09 UTC

    You need to show the actual code you tried. Much, much better is to rework your code into a stand alone test script so we can run exactly what you are running with the same data. Something like:

    #!/usr/bin/perl use strict; use warnings; my $keyword = 'file'; # Search for the key pattern in the file information. while (defined (my $line = <DATA>)) { next if $line !~ /\b\Q$keyword\E\b/; print "Found key word $keyword on line $.\n"; exit; } print "Keyword not found\n"; __DATA__ Hi, I am a newbie to perl. I am trying to pass the filename and search + for a keyword in that file and if found print that line else print not found + message. However though my file has the keyword I get not found message.Please +help.I

    Prints:

    Found key word file on line 2
    True laziness is hard work
      Here's the entire code and the file which I am giving as input for parsing.
      #!/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";
      -----------------------------------------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).
      Controller 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
      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 . - Ram

        Ok. A fairly full on version of that looks like this:

        #!/usr/bin/perl use strict; use warnings; use Getopt::Long; my %options; Getopt::Long::GetOptions(\%options, 'all', 'keyword:s', 'help'); my @filenames = @ARGV; if (!exists $options{keyword} || !@filenames || exists $options{help}) + { print <<HELP; search [--all] --keyword <match word> <file list> Searches all files in <file list> and prints the lines where <matc +h word> is found. If --all is provided the entire file containing <match w +ord> is printed. HELP exit; } my $found; for my $fileName (@filenames) { open my $inFile, '<', $fileName or die "Unable to open $fileName: +$!\n"; while (defined(my $line = <$inFile>)) { next if $line !~ /\b\Q$options{keyword}\E\b/; if ($options{all}) { print "\nKeyword $options{keyword} found on line $. of $fi +leName\n"; seek $inFile, 0, 0; print <$inFile>; $found = 1; last; } print $line; $found = 1; } close $inFile; } print "Keyword not found in ", join(', ', @filenames), "\n" if !$found +;

        There are a few things to note in that code:

        1. The command line is used to give switches and the list of files to process
        2. The module Getopt::Long is used to process command line switches
        3. The script gives help if it doesn't like the command line parameters or help is asked for using the --help switch
        4. Three parameter open is used and the result checked.
        5. Lexical file handles (my $inFile) are used
        6. seek is used to set the input file back to the start so a simple print <$inFile>; can be used to print the entire file
        7. A found flag is used to keep track of wether a match has been found or not.

        A typical command line looks like:

        search --keyword "Boot Flash" --all file1.txt file2.txt
        True laziness is hard work