in reply to Re^4: Error using grep
in thread Error using grep
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:
A typical command line looks like:
search --keyword "Boot Flash" --all file1.txt file2.txt
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Error using grep
by justkar4u (Novice) on Mar 29, 2011 at 23:00 UTC |