Different Uses of the “Grep” Command
The uses of the “grep” command to search the content in a file and array are shown in this part of the tutorial using three examples.
Example 1: Use of the “Grep” Command to Search the Values in a File
Create a text file named “languages.txt” using the following content that is used in the Perl script:
languages.txt
Python
Perl
PHP
Java
C++
Perl
VB.NET
Create a Perl file with the following script that takes the filename and search string from the user to search the string value in the file content. The open() function is used in the script to open the file to read if the filename that is taken from the user exists in the current location. Next, read the full content of the file and search for the word that is taken from the user in the content using the “grep” command.
use warnings;
use v5.34.0;
say "Enter the filename: ";
#Take file name to search
my $filename = <>;
chomp($filename);
say "Enter the search string: ";
#Take the search string
my $search = <>;
chomp($search);
#Open file for reading
if(open(file_handler, $filename))
{
#Read all content of the file
my @content=<file_handler>;
close file_handler;
#Search the searching string in the file
my @result=grep /$search/,@content;
#Count the length of the array
my $len = @result;
#print message based on the search result
if($len > 0)
{
print "The '$search' word exist $len time(s) in the file.\n";
}
else
{
print "No matching word found.\n";
}
}
else
{
print "File does not exist.\n";
}
Output:
The following output appears after executing the script with the “languages” filename and “Perl” search string. Here, the filename is wrong:
The following output appears after executing the script with the “languages.txt” filename and “Drupal” search string. Here, the filename is correct but the string value does not exist in the file:
The following output appears after executing the script with the “languages.txt” filename and “Perl” search string. Here, the filename is correct and the string value exists in the file:
Example 2: Use of the “Grep” Command to Search a Content in an Array of Numbers
Create a Perl file with the following script that takes a number from the user and search for the number in an array. An array of 8 numbers is declared in the script. The first “grep” command is used in the script to search the input number in the array. The second “grep” command is used in the script to search the numbers in the array that are greater than 25 and less than or equal to 50.
use warnings;
my @numbers = (23, 6, 31, 89, 56, 22, 9, 45);
print "Enter the search number: ";
#Take search string
my $n = <>;
chomp($n);
#Search the particular number
my @search_result = grep { $_ == $n } @numbers;
if (@search_result)
{
print "$n exists in the array.\n";
}
else
{
print "$n does not exist in the array.\n";
}
#Search the range of numbers
@search_result = grep { $_ > 25 && $_ <=50 } @numbers;
print "The list of numbers less between 25 to 50 are:\n @search_result\n";
Output:
The following output appears after executing the script with the number 23:
The following output appears after executing the script with the number 44:
Example 3: Use of the “Grep” Command to Search a Content in an Array of Strings
Create a Perl file with the following script that takes a string value from the user and search for the value in the array. An array of strings ia declared in the script. The “grep” command is used in the script to search the input string in the array and compare the input values with the other two string values. The message is printed based on the search result and the comparison result of the “grep” command.
use warnings;
#Declare an array of strings
my @students = ('Milon Hossain', 'Sangita Das', 'Ria Moni', 'Shihabudddin', 'Tanzin Tisa');
print "Enter the student name: ";
#Take the input
my $name = <>;
chomp($name);
#Search the name
my @search_result = grep { $_ eq $name && ($name eq 'Milon Hossain' || $name eq 'Ria Moni') } @students;
#Print message based on the search result
if (@search_result)
{
print "$name passed the exam.\n";
}
else
{
print "$name didn't pass the exam.\n";
}
Output:
The following output appeats after executing the script with the input value of “Ria Moni”:
The following output appears after executing the script with the input value of “Tarek Chowdhury”:
Conclusion
The uses of the “grep” command based on one or more conditions are shown in this tutorial.