Project #6 Perl Part A- find.pl Create the script find.pl which searches for fil
ID: 3601683 • Letter: P
Question
Project #6 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 -ioption 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/Proj For part A, copy those into a directory named "DataA". Example: For a directory, DataA, containing the following files input.txt projPer11.input projz.c projz.h Assuming input.txt contains the following text proj1.c proj1.o proj12.c projPer11.pl trash.txt 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 proji proji.c proj1.o proj12.c $ find.pl -i ". *proj1.*" DataA/* projPerl1.pl projPerl1.input projz.c projZ.h trash.txtExplanation / Answer
FOR CURRENT DIRECTORY:
#!/usr/bin/perl -w
opendir(DIR, ".");
@files=grep(/"expression",readdir(DIR));
openfile(FILE,".");
@contents=grep(/"expression",readfile(FILE));
closefile(FILE);
closedir(DIR);
for each $file(@files){
print "$file ";
for each $content(@contents){
print "$file ";
}
}
FOR GLOBAL:
#!/usr/bin/perl -w
@files=glob("*expression*");
foreach $file(@files){
print "$file " if -f $file;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.