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 doesnt 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 Ifthe -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 projPer11.input projz.c Assuming input.txt contains the following text proji.c proj1.o proj12. projPerl1.pl projz.h 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 proj1 proj1.c proj1.o proj12.c $ find.pl -i . *po1.*DataA/* projPer11.p projPerl1.input projz.c projz.h trash.txtPart 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 projPer11.input projz.c projz.h proji.cproj1. proj12.c projPerI1.perl trash.txt Executing: $ projectSorter DataB produces the following file structure in DataB assignment1/ proji.c proji.o proj12.oc projPer11.perl assignment12/ assignmentPerl1/ 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 o find.pl Perl script for part A. projectSorter.pl- Perl script for part B o

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code-1:

Unix Terminal> cat find.pl

#!/usr/bin/perl

use strict;

use warnings;

# validating if the given argument has atleast 2 arguments passed

if (@ARGV < 2){

print "Invalid argument. Atleast two arguments must be passed", "\n";

print "Usage $0 <perlRegex> <List of files>", "\n";

}

# if all the required arguments are passed

else{

# if -i option is passed

if($ARGV[0] eq "-i"){

# taking regex and dir name into the variables

my ($regex, $dir) = ($ARGV[1], $ARGV[2]);

# capturing directory list

my @dirList = glob $dir;

# iterating through files in directory

foreach my $file(@dirList){

# flag variable to verify if the filename does not contain regex in its name or contents

my $flag = 1;

# capturing the filename without complete path

my $basename = (split /\//, $file)[1];

# if filename does not contain the regex given

if($file =~ /$regex/){

$flag = 0;

}

else{

# opening the file in read mode and checking if file does not have the required regex

open(FH, "<" , "$file") or die "$file cannot be opened: $!";

# flag variable to check if the given regex is not in entire file

while(my $line = <FH>){

if($line =~ /$regex/){

$flag = 0;

last;

}

}

close(FH);

}

# if filename does not contain the regex match or in its content then flag variable will be 1 otherwise 0

if($flag){

print $basename, "\n";

}

}

}

else{

# taking regex and dir name into the variables

my ($regex, $dir) = ($ARGV[0], $ARGV[1]);

# capturing directory list

my @dirList = glob $dir;

# iterating through files in directory

foreach my $file(@dirList){

# capturing the filename without complete path

my $basename = (split /\//, $file)[1];

# if filename contains the regex given

if($file =~ /$regex/){

print $basename, "\n";

}

else{

# opening the file in read mode and checking if file contains the required regex

open(FH, "<" , "$file") or die "$file cannot be opened: $!";

while(my $line = <FH>){

if($line =~ /$regex/){

print "$basename: $line";

last;

}

}

close(FH);

}

}

}

}

Unix Terminal>

Code output screenshot:

Unix Terminal> ./find.pl Invalid argument. Atleast two arguments must be passed Usage ./find.pl <perlRegex> <List of files> U

Code-2:

Unix Terminal> cat projectSorter.pl

#!/usr/bin/perl

use strict;

use warnings;

# validating if the given argument is atleast one argument passed

if (@ARGV != 1){

   print "Invalid argument. One argument must be passed", "\n";

   print "Usage $0 <directory>", "\n";

}

else{

   # retreiving one argument which is dirname

   my $dirname = $ARGV[0];

   # getting list of files from the given directory

   my @files = glob $dirname . '/*';

   # iterating through each file in given directory

   foreach my $file(@files){

       # getting the basename of file without /

       my $baseName = (split /[\/]/, $file)[1];

       # retreiving the name before extension if filename contains proj*

       if($baseName =~ /proj(.*)\./){

           # creating a subdirectory from the filename

           my $assign_name = $1;

           my $sub_dirname = $dirname . "/" . "assignment" . $assign_name;

           # creating a subdirectory

           unless(-e $sub_dirname and -d $sub_dirname){

               `mkdir $sub_dirname`;

           }

           # moving the file to sub directory

           `mv $file $sub_dirname`;

       }

       # creating a directory misc if filename does not contains proj*

       else{

           my $sub_dirname = $dirname . "/" . "misc";

           unless (-e $sub_dirname and -d $sub_dirname){

               `mkdir $sub_dirname`;

           }

           `mv $file $sub_dirname`;

       }

   }

   print "\n";

}

Unix Terminal>

Code output screenshot:

Unix Terminal> ls -tlrh total 16 drwxr-xr-x 11 vishal group 374B Oct 26 20:21 DataA -rxr-xr-x 1 vishal group 2.4K Oct 26 21:1

Add a comment
Know the answer?
Add Answer to:
Project #6 Perl This assignment has two parts. Each part must be done using Perl Part...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  •  Write a Perl script that accepts exactly 2 integer arguments where the first argument must...

     Write a Perl script that accepts exactly 2 integer arguments where the first argument must be less than the second argument. The script will print a comma separated list of integers starting with the first argument up through the second argument.  The last printed value should be the second command line argument not be followed by a comma.  The script should also be able to handle the following errorsituations: o incorrect number of arguments o the first...

  • Your mission in this programming assignment is to create a Python program that will take an...

    Your mission in this programming assignment is to create a Python program that will take an input file, determine if the contents of the file contain email addresses and/or phone numbers, create an output file with any found email addresses and phone numbers, and then create an archive with the output file as its contents.   Tasks Your program is to accomplish the following: ‐ Welcome the user to the program ‐ Prompt for and get an input filename, a .txt...

  • In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to...

    In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to extract all matching patterns (substrings) from a given input DNA sequence string. The alphabet for generating DNA sequences is {A, T, G, C}. Write a regular expression that represents all DNA strings that contains at least two ‘A’s. Note: assume empty string is not a valid string. Design a deterministic finite automaton to recognize the regular expression. Write a program which asks the user...

  • Mini Project You are required to do this assignment on your own skills. No copying from...

    Mini Project You are required to do this assignment on your own skills. No copying from other students are not allowed. Write a menu driven Bash script using the guidelines given below. Some of the commands and syntax may not be covered in lectures. Please refer other sources to understand syntax and commands you need to complete this assignment. This program takes user input and to perform simple arithmetic operations such as addition, subtraction, multiplication and division of any two...

  • /*hello everyone. my question is mostly related to the garagetest part of this assignment that i've...

    /*hello everyone. my question is mostly related to the garagetest part of this assignment that i've been working on. i am not sure how to read the file's contents AND put them into variables/an array. should it be in an arraylist? or maybe a regular array? or no arrays at all? i am also not sure how to call the factory method from garagec. i'm thinking something along the lines of [Garage garage = new GarageC.getInstance("GarageC", numFloors, areaofEachFloor);] but i...

  • For this project, you are tasked with creating a text-based, basic String processing program that performs...

    For this project, you are tasked with creating a text-based, basic String processing program that performs basic search on a block of text inputted into your program from an external .txt file. After greeting your end user for a program description, your program should prompt the end user to enter a .txt input filename from which to read the block of text to analyze. Then, prompt the end user for a search String. Next, prompt the end user for the...

  • C++ please Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require...

    C++ please Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a file. The encryption method being used on the file is called a shift cipher (Info Here). I will provide you with a sample encrypted message, the offset, and the decrypted message for testing. For this project I will provide you main.cpp, ShiftCipher.h, and ShiftCipher.cpp....

  • Overview: The goal of this assignment is to implement a simple spell checker using a hash...

    Overview: The goal of this assignment is to implement a simple spell checker using a hash table. You will be given the basic guidelines for your implementation, but other than that you are free to determine and implement the exact classes and methods that you might need. Your spell-checker will be reading from two input files. The first file is a dictionary containing one word per line. The program should read the dictionary and insert the words into a hash...

  • C++ There are to be two console inputs to the program, as explained below. For each input, there is to be a default val...

    C++ There are to be two console inputs to the program, as explained below. For each input, there is to be a default value, so that the user can simply press ENTER to accept any default. (That means that string will be the best choice of data type for the console input for each option.) The two console inputs are the names of the input and output files. The default filenames are to be fileContainingEmails.txt for the input file, and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT