# This script is used to count the number of occurrence of a given search word in the files provided in the command line.
# The user has to provide the file names as parameters in the command line.
#!/usr/local/bin/perl
# Declaring the variables
my $wordToSearch;
my $fileCount=0;
my @wordCount;
my $totalWordCount=0;
# Declaring the variables for sending mail
my $fromaddress = “sreejith_ar\@infosys.com”;
my $toaddress = “to_dave\@infosys.com”;
my $subject = “Give some subject”;
my $mailer = “/usr/lib/sendmail -f$fromaddress -oi”;
#Prompting the user to enter the word to search and storing it in variable.
print(”Enter the word to be searched:”);
$wordToSearch=<STDIN>;
#Choping the input to get rid of the new line character at the end
chop ($wordToSearch);
#Starting the loop which will continue till the files given in the command line is exhausted.
while ($fileCount < @ARGV)
{
$wordCount[$fileCount]= 0;
#Tries to open the file one by one. If the file cannot be opened then the program will print an error message
unless (open (INFILE, $ARGV[$fileCount]))
{
die (”Cannot open the input file $ARGV[$fileCount]\n”);
}
#If the program reached untill here that means the file was available to open.
#The loop continues untill the end of file is reached.
while ($line = <INFILE>)
{
my $count=0;
#Chop the line for removing the new line character.
chop($line);
#Split the line based on space and stores it in the array @array
my @array = split(/ /,$line);
#The loop continues until it reaches the end of the array
while($count < @array)
{
#Checks the array one by one to find whether the elements matches with the search words.
#If matches then increments the word count.
if ($array[$count] eq $wordToSearch)
{
$wordCount[$fileCount]= $wordCount[$fileCount]+1;
}
$count=$count+1;
}
}
#Count the total number of words in all the files.
$totalWordCount=$totalWordCount+$wordCount[$fileCount];
#Print the result.
print (”The Number of Occurance of the word \”$wordToSearch\” in the file \”$ARGV[$fileCount]\” is: $wordCount[$fileCount]\n\n”);
$fileCount=$fileCount+1;
}
print(”The total number of Ocuurance of the word \”$wordToSearch\” in all the files is : $totalWordCount \n\n”);
#Below is the optional code which you can add if you want to send a mail with the details.
open (MAIL, “|$mailer -t”) || die “Can’t open mailer”;
print MAIL “To: $toaddress\n”;
print MAIL “cc: \n”;
print MAIL “Subject: $subject\n\n”;
$count=0;
while($count < @ARGV)
{
print MAIL “The Number of Occurance of the word \”$wordToSearch\” in the file \”$ARGV[count]\” is: $wordCount[count]\n\n”;
}
print MAIL “The total number of Ocuurance of the word \”$wordToSearch\” in all the files is : $totalWordCount \n\n\n\n”;
print MAIL “\n\nThis email was system generated, please do not reply.”;
print MAIL “\n\n\n\n\n\n\n\n\n\n\n\nThis e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.”;
close (MAIL);
Tags: coding tips, Perl, PERL Script -A word search program, perl scripts, scripts, technical FAQ, technical tips