Project #6 Perl This assignment has two parts. Each part must be done using Perl
ID: 3595149 • Letter: P
Question
Project #6 Perl
This assignment has two parts. Each part must be done using Perl.
Part A – find.pl
Create the script find.pl which searches for files in a directory list based on a regular expression pattern:
If the file name matches, print that file name.
If the file name doesn't match, it should look for instances of the regular expression within the text of the file. If found, it should print the filename, a colon, and the text for the first line that contained that pattern.
If the -i option is passed, the script should return the opposite files (i.e., those which do not match the expression in file name or contents). The script should be invoked with the following format:
find.pl -i perlRegexpPattern listOfFiles
where -i is optional.
Example data is located in /usr/local/courses/clark/cs3423/2017Fa/Proj6/
For part A, copy those into a directory named "DataA".
Example: For a directory, DataA, containing the following files:
input.txt proj1.c proj1.o proj12.c projPerl1.pl
projPerl1.input projZ.c projZ.h trash.txt
Assuming input.txt contains the following text:
Hello World
This is input for proj1
Test data
find.pl should behave in the following way (the order of the files is insignificant):
$ find.pl ".*proj1.*" DataA/*
input.txt:This is input for proj1
proj1.c
proj1.o
proj12.c
$ find.pl -i ".*proj1.*" DataA/*
projPerl1.pl
projPerl1.input
projZ.c
projZ.h
trash.txt
Part B – projectSorter.pl
Assume you have a directory filled with random files including some project files beginning with the string "proj" (e.g., proj1.c, projB.o, proj12.h) and no directories. Create a script which places each file into a corresponding project directory titled “assignmentXXX” where XXX is replaced with whatever follows the string “proj” in the file, not including the extension. Any other files not beginning with “proj” should be placed into a “misc” directory.
The script should be invoked with the following format:
projectSorter.pl directory
Example data is located in /usr/local/courses/clark/cs3423/2017Fa/Proj6/
For part B, copy those into a directory named "DataB".
Example: For a directory, DataB, containing the following files:
input.txt proj1.c proj1.o proj12.c projPerl1.perl
projPerl1.input projZ.c projZ.h trash.txt
Executing:
$ projectSorter DataB
produces the following file structure in DataB:
assignment1/
proj1.c
proj1.o
assignment12/
proj12.c
assignmentPerl1/
projPerl1.perl
projPerl1.input
assignmentZ/
projectZ.c
projectZ.h
misc/
input.txt
trash.txt
Notes for partB:
The glob function can be useful in getting the contents of the specified directory.
You will probably need to use `expression` to invoke Linux commands to create directories and move files.
What to turn in
A LastnameFirstname.zip file containing:
find.pl – Perl script for part A.
projectSorter.pl – Perl script for part B.
Explanation / Answer
Find.pl
#!/usr/local/courses/clark/cs3423/2017Fa/Proj6/
use strict; @filename;
$i = 0;
$total;
open(INFILE,"input.txt");
while($filename[$i] = <INFILE>){
chop($filename[$i]);
$filename[$i] =~ s/*//g;
$i++;
$total++;
}
$error = open(OUTFILE,"> dict.html");
if(!$error){ print("ERROR ");
exit(0);
}
print(OUTFILE "<HTML> <HEAD> </HEAD> <BODY TEXT=#00ff00 LINK=# +00ff00 VLINK=#00ff00 BGCOLOR=#000000 alink=00FF00> ");
for($i=0;$i<=$total;$i++)
{
print(OUTFILE "<A href="$filename[$i]">$filename[$i]</A><BR> ");
}
print(OUTFILE "</BODY> </HTML> "); close(OUTFILE);
projectSorter.pl
Actually this is not acyually what you want but this is the way to solving your problem.
Are the files all in that one folder, or are they in subfolders? If they are all in a single folder, you can use opendir to access the directory, and then readdir to read the file names and copy them elsewhere (using File::Copy module's move or copy function.) The following is the way to write a program accordingly. globe function , and try to avoid that, insteadof globe use readdir and closedir.
further you can use the following code to
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.