Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project #6 Perl This assignment has two parts. Each part must be done using Perl

ID: 3600567 • 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

Explanation / Answer

Code for projectSorter.pl :

#!/usr/bin/env perl

use strict;
use warnings;
use File::Copy;
#Variable declaration done one for source variable reading from script parameter and other for misc folder

my $source_dir = $ARGV[0];
my $target_dir = "/misc";
#Now by using grep condition get thet both file names list (one to misc other depending on file name)

my @files_Name_proj = grep ( -f ,<proj*>) read_dir $source_dir;

my @files_Name_Not_proj = grep ( -f ,<-v proj*>) read_dir $source_dir ;

#Move all files to misc folder for files not starting with proj.

foreach my $t (@files_Name_Not_proj)

{
   if(! -f "$source_dir/$t" ) {
      #Check with -f only for files (no directories)
      copy "$source_dir/$t", "$target_dir/$t";
   }
}
#Create new directory and move files starting with proj according to the filename projXXX

foreach my $t (@files_Name_proj)

{
   if(! -f "$source_dir/$t" ) {
my $Assignment_Index = split 'proj', $t;
      #Check with -f only for files (no directories)
      copy "$source_dir/$t", /assignment."$Assignment_Index";
   }
}