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

In reply to Re^5: 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.