#!/usr/bin/perl use strict; ################################################## #file: #This file runs a grep-style program. It takes an infile, searches it, and prints the result to an outfile. #This program looks for the word "penguin" in a text file. #date: # #Author: ################################################## #declare variables my $infile; my $outfile; my $number; #check number of arguments if (@ARGV != 2) { die "\nUsage: perl search.pl infile outfile\n\n"; } # Get the file names and open filehandles. $infile = shift; $outfile = shift; open IN, $infile or die "Couldn't open file: $infile\n"; open OUT, ">$outfile" or die "Couldn't open file: $outfile\n"; # Read in the contents of the file line by line, looking for a match lower or uppercase # keep track of the number of lines with matches while () { if (/penguin/i) { $number++; } } #if there was at least one match, print the success message and print a more detailed message to the outfile. #otherwise print the fail message. if ($number != 0) { print "Penguins in your text! Number of lines with \"penguin\" printed to $outfile\n\n"; print OUT "There are $number lines with the word \"penguin\" in your text." } else { print "No penguins found!!!\n\n"; }