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

Create a Perl program that is simply a loop over a set of \"Regular Expressions\

ID: 3839523 • Letter: C

Question

Create a Perl program that is simply a loop over a set of "Regular Expressions" that perform the tasks described below. Use the attached file SampleConfig.txt as input.

Note: Though this is a bit assignment is a bit contrived, it does represent the types of actual text manipulations that might be performed over many large files.

Instructions:

Using the attached SampleConfig.txt file, provide a Perl regular expression for each of the following tasks:

Remove any leading or trailing space from each line.
  

Replace all of the occurrences of a full line of # characters (ignoring leading or training spaces and lines with just 1 #) with the following: # ---------- #
  

Change any URL ending in .htm to end with .html
  

For any set of multiple words following an equal sign (=), place what follows the equal sign in quotes.

eg: a = bcd efg becomes  a = "bcd efg"

eg: a = bcd  remains a = bcd
  

Add a semicolon (;) to the end of every line.
  

Every line beginning with a comment marker (#) and containing additional text should have one (and only one) space following the comment marker.

For example:
   # some text      becomes....
   # some text

  

Replace any pattern in the form of [__UPPER-CASE CHARACTERS__] with (lower-case characters)

ie: Open square bracket + two underscores + upper-case text +  two underscores +  close square bracket

eg:[__SOMEDATA__] becomes [somedata]
  

Follow every "config section" with a comment that indicates the current section count:

For example:
  [SVN] # Section: 1
[
......] # Section: 2
  etc.

Explanation / Answer

#!/usr/bin/perl -w

use strict;

open(FILE, "</tmp/SampleConfig.txt") || die "File not found";
my @filelines = <FILE>;
close(FILE);

my @eachLine;
foreach(@filelines) {
$_ =~ s/#/#-----------#/g;
push(@eachLine,$_);
}

open(FILE, ">/tmp/SampleConfigTemp.txt") || die "File not found";
print FILE @eachLine;
close(FILE);