Question
NEED HELP with LINUX! Please write a perl script, searchone.pl, that will search a given data file for a given pattern. Both the search pattern
NEED HELP with LINUX!
Please 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.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Example 2.1: read a file name from the command line and then display the files content:
$ cat > lab9b.pl #!/usr/bin/perl -w
open(FILE,$ARGV[0]) or die("Could not open the file $ARGV[0]");
while ( $line =
print $line;
}
close(FILE);
Example 2.2: read a file name from the command line and then display the files content based on a pattern search:
$ cat > lab9b2.pl
#!/usr/bin/perl -w
open(FILE,$ARGV[0]) or die("Could not open the file $ARGV[0]");
while ( $line =
if ( $line =~ /bash/ ) {
print $line;
}
}
close(FILE);
Example 2.3: read a file name and a pattern from the command line and then display the files content based on a search of the pattern:
$ cat > lab9b3.pl #!/usr/bin/perl -w
open(FILE,$ARGV[0]) or die("Could not open the file $ARGV[0]");
while ( $line =
if ( $line =~ /$ARGV[1]/ )
{
print $line;
}
close(FILE)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started