Perl

Perl Grep Examples

The “grep” command is used in Linux to search the specific files or folders or the particular content from a file. The “grep” command in Perl is used to find out the filtered list from a list of data based on a particular condition or pattern. This command can be used also to find out the lines from a file based on the particular regular expression pattern. So, you should know about writing the regular expression pattern to use regex with the “grep” command in Perl. The ways of using the “grep” command in Perl are shown using multiple Perl scripts in this tutorial.

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

Bash

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.

#!/usr/bin/perl

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:

p1-1

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:

p1-2

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:

p1-3

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.

#!/usr/bin/perl

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:

p2-1

The following output appears after executing the script with the number 44:

p2-2

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.

#!/usr/bin/perl

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”:

p3-1

The following output appears after executing the script with the input value of “Tarek Chowdhury”:

p3-3

Conclusion

The uses of the “grep” command based on one or more conditions are shown in this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.