Question

In: Computer Science

Write a program named "cleanDocument.pl" that will do the following: Reads two parameters from the command...

Write a program named "cleanDocument.pl" that will do the following:

  • Reads two parameters from the command line (@ARGV)
    • The first parameter is the name of a file and the second parameter is a word
    • If no command line parameters are present, it asks the user to input the values
  • Opens the text file that is passed as the first command line argument
  • Loops through the lines of the text file and replaces all occurrence of the word (the second command line parameter) with an equal number of dashes (that is the same amount of dashes as the word length)
  • It then overwrites the file with the new replaced data

Below is an example of how the program works:

Calling the Perl file on the command line like so:

$ cleanDocument.pl document.txt bobsled

or

$ cleanDocument.pl
$ What file do you want to open? document.txt
$ What word do you want to replace? bobsled

If the file looks like below:

$ cat document.txt

I like to use my bobsled during the winter. Whenever I bobsled I have a lot of fun.

Then it will be become:

$ cat document.txt

I like to use my ------- during the winter. Whenever I ------- I have a lot of fun.

Solutions

Expert Solution

Given question, let us implement following points in question as cleanDocument.pl, Below is Solution perl cleanDocument.pl snippet with detailed explaination using comments,

use warnings;

# take input argument file name and word to replace
my ($fileName, $word) = @ARGV;

if(not defined $fileName)
{
    # request file name to user as specified in question.
    print "What file do you want to open? ";
    $fileName = <STDIN>;
    
    # Remove new line end of input  
    chomp $fileName;
}
if(not defined $word)
{   
    # as specified in question prompt user to enter word name
    print "What word do you want me to replace? ";
    $word =  <STDIN>;
    chomp $word;
}


@newLines = ();
# open file, throw error and exit if no file exist
open(my $fhr, '<', $fileName) or die "file '$fileName' not present $!\n";
while (my $file_data = <$fhr>)
{
    chomp $file_data;

    # for word replacement with equal number of dashes
    $word_replacement = "-" x length($word);
    if($file_data =~ m/$word/)
    {
        # if string matches with specified word,
        # replace word with word_replacement store in file_data variable.
        $file_data =~ s/$word/$word_replacement/g;
    }
    # write newLines with new file data
    push @newLines, $file_data;
}

open(my $fhw, '>', $fileName) or die "Could not open file '$fileName' $!\n";
# for each new lines, wite file with modified lines
for(@newLines)
{
    print $fhw "$_\n";
}

Output:

Test for no command line parameters,


Related Solutions

Write a program that contains the following Write a function copies with two int parameters, named...
Write a program that contains the following Write a function copies with two int parameters, named n and x. It should dynamically allocate a new array of size n.  The function should then copy the value in x to every position in the array. The function should return a pointer to the new array. In the main function, call the copies function from part 1. with size 5 and value -1.  Output the resulting array to the screen, and deallocate the array....
Write a JavaScript program with a function named fives that reads two numbers from two text...
Write a JavaScript program with a function named fives that reads two numbers from two text fields and then outputs to a div "True" if both of the numbers are greater than 5 or if their sum is greater than 20. Otherwise your function should output "False" to the div. If you wish, you may use the following HTML code to begin your program.
Use C++ to write a program that reads in a binary string from the command line...
Use C++ to write a program that reads in a binary string from the command line and applies the following (00, 1101) tag-system: if the first bit is 0, deletes the first three bits and append 00; if the first bit is 1, delete the first three bits and append 1101. Repeat as long as the string has at least 3 bits. Try to determine whether the following inputs will halt or go into an infinite loop: 10010, 100100100100100100. Use...
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Turtle Command Interpreter You will write a program that reads a sequence of codes, converts them...
Turtle Command Interpreter You will write a program that reads a sequence of codes, converts them into Turtle commands, and then executes them. The codes are in a Python list, passed as an argument to your function. turtleRunner(t, x) t is a Turtle x is a list of Turtle commands Code List (f, d)    Move forward by d pixels u Lift pen up d Put pen down (l, d) Turn left by d degrees (r, d) Turn right by...
PYTHON: Turtle Command Interpreter You will write a program that reads a sequence of codes, converts...
PYTHON: Turtle Command Interpreter You will write a program that reads a sequence of codes, converts them into Turtle commands, and then executes them. The codes are in a Python list, passed as an argument to your function. turtleRunner(t, x) t is a Turtle x is a list of Turtle commands Code List (f, d)    Move forward by d pixels u Lift pen up d Put pen down (l, d) Turn left by d degrees (r, d) Turn right...
You need to write a program that reads in two integers from cin and outputs an...
You need to write a program that reads in two integers from cin and outputs an horribly inefficent calculation of the median value. First count from the first number to the second, but stop one short. Then, count from that number back towards the first, again stopping one short. Continue until you reach a single number. Enter 3 and 9 solution: 3456789 987654 45678 8765 567 76 6
You need to write a program that reads in two integers from cin and outputs an...
You need to write a program that reads in two integers from cin and outputs an horribly inefficent calculation of the median value. First count from the first number to the second, but stop one short. Then, count from that number back towards the first, again stopping one short. Continue until you reach a single number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT