Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Perl script that prints the contents of a file, prefixing each line with a line number. The script should accept exactly one argument

Write a Perl script that prints the contents of a file, prefixing each line with a line number. The script should accept exactly one argument representing the name of the file.

The script should be able to handle the following error conditions:

o Incorrect number of arguments

o The specified argument is not a file.

You cannot use the UNIX cat command in this assignment. You must use Perl to read the file and count the lines.

You cannot use the UNIX nl command in this assignment.

The printf function in Perl is useful for formatting numbers.

The script file name must be: linenum.pl

The script must be located in $HOME/itec400/homework

Make sure the permissions on the your itec400 directory are 705

Make sure the permissions on your script are 705

image text in transcribed

image text in transcribed

> linenum.pl /export/home/dandrear/public_html/itec400/examples/ex0330 0001: #!/bin/perl -w 0002: 0003: ## # # # # # # # # # 0004: # File: ex0330 0005: # 0006: # A simple Perl script that demonstrates 0007: # the use of hash (plural) variables 0008: ### ### ### ## 0009: 0010: %states 1 = ( "oh", "Ohio", "fl", "Florida", 0011: "de", "Delaware" ); 0012: print "I live in $states_1{oh} "; 0013: 0014: $states_2 = ( 0015: "oh" => "Ohio", 0016: "fl" => "Florida", 0017: "de" => "Delaware" 0018: ); 0019: print "I was born in $states 2{de} "; Rainy Day Scenarios: > linenum.pl error: program must be executed with 1 argument. usage: linenum.pl filename > linenum.pl a b error: program must be executed with 1 argument. usage: linenum.pl filename > linenum.pl /etc error: argument must be a file usage: linenum.pl filename ***Please use the template below*** #!/usr/bin/PROGRAM STATEMENT Script name: linenum.pl Case #1: ./linenum.pl Check for the correct number of arguments unless (SYNTAX) { print "error: incorrect number of arguments", " ", "usage: linenum.pl [filename]", " "; exit VALUE; Open function used with filehandle and input file. COMMAND (MY_FILE, "SARGV[INDEX]") or die "error: argument must be a file ", "usage: linenum.pl [filename] $! "; Check if file is a directory if (-SYNTAX "$SYNTAX[INDEX] ") { print "error: argument must be a file", "usage: linenum.pl [filename] "; exit VALUE; SCOUNTER=1; # Used for printing line numbers Loop and write lines from file SYNTAX (SLINEEQUALITY OPERATOR) { # Adds leading zeros for numbers 1 digit long if ($COUNTER9) && (SCOUNTER99) && (SCOUNTER

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 2005 Databases

Authors: Karli Watson

1st Edition

0470044063, 978-0470044063

More Books

Students also viewed these Databases questions