Question: Please write a perl script, searchtwo.pl, that will search a given data file for a given pattern . Both the search pattern and the search
Please write a perl script, searchtwo.pl, that will search a given data file for a given pattern. Both the search pattern and the search file name should be obtained from the prompted inputs
$ ./searchtwo.pl
Enter a data file name: /etc/services
Enter a search pattern: pop
$ ./searchtwo.pl
Enter a data file name: /etc/services
Enter a search pattern: imap
You might want to review Example 5 in Lab 8 and Examples 2.1 to 2.3 in Lab 9 before writing up your script sesearchtwo.pl;
Please test your script to make sure it displays the same information as you used grep for the two 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);
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Example 5: This example will show you how to get two numbers from the keyboard and then to output the summation of the two numbers.
$ cat > myperladd2.pl
#!/usr/bin/perl -w
print "Please enter the first number: ";
$first = <>;
print "Please enter the second number: ";
$second = <>;
$total = $first + $second;
chomp($first);
chomp($second);
print "$first + $second = $total ";
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
