#!/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 < Searches all files in and prints the lines where is found. If --all is provided the entire file containing 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 $fileName\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; #### search --keyword "Boot Flash" --all file1.txt file2.txt