Write a perl script, searchone.pl, that will search a given data file for a given pattern. Both the search pattern and the search file name should be provided from the command line as follow:
searchone.pl search-pattern search-file
Examples (Lab 9, questions 1 to 3) or the followings
$ ./searchone.pl pop /etc/services
$ ./searchone.pl imap /etc/services
You might want to review Examples 2.1 to 2.3 in Lab 9 before coming up your script searchone.pl
Please test your script to make sure it displays the same information as you used grep for those search patterns and the file in Lab 9.
Perl Script: searchone.pl
Here's a simple Perl script named searchone.pl that searches a given data file for a specified pattern. This script takes two command-line arguments: the search pattern and the file name. It will read the file and print out lines that match the search pattern, similar to the behavior of the grep command.
Perl Script: searchone.pl
#!/usr/bin/perl
use strict;
use warnings;
# Check if the correct number of arguments are provided
if (@ARGV != 2) {
die "Usage: $0 search-pattern search-file\n";
}
my ($pattern, $file) = @ARGV;
# Check if the file exists and is readable
unless (-e $file && -r $file) {
die "File '$file' does not exist or cannot be read.\n";
}
# Open the file for reading
open my $fh, '<', $file or die "Cannot open file '$file': $!\n";
# Search for the pattern in each line of the file
while (my $line = <$fh>) {
if ($line =~ /$pattern/) {
print $line; # Print the matching line
}
}
# Close the file handle
close $fh;
How to Use the Script
1. Save the Script: Copy the above code into a text file and save it as searchone.pl.
2. Make it Executable: You need to give execute permissions to your script. You can do this by running:
chmod +x searchone.pl
3. Run the Script: You can run the script from the command line by providing a search pattern and a file name. For example:
./searchone.pl pop /etc/services
./searchone.pl imap /etc/services
Explanation of the Script
- The script starts by checking if exactly two arguments are provided. If not, it prints a usage message and exits.
- It assigns the command-line arguments to $pattern and $file.
- The script checks if the specified file exists and is readable. If not, it exits with an error message.
- It opens the file for reading and uses a while loop to read through each line of the file.
- Inside the loop, it checks if the line matches the given pattern using a regular expression. If it does, it prints the line.
- Finally, it closes the file handle.
Testing the Script
To ensure that your script behaves like grep, you can test it against known patterns and files. For example, compare the output of your script with:
grep 'pop' /etc/services
grep 'imap' /etc/services
Make sure to check both matching and non-matching patterns to validate that your script is functioning correctly.
By following these instructions, you will have a fully functional Perl script that emulates a basic version of grep for searching patterns in files.