13.0k views
25 votes
Write a program that reads a file containing text. Read each line and send it to the output file, preceded by line numbers. If the input file is Mary had a little lamb Whose Fleece was a white as snow. And everywhere that Mary went, The lamb was sure to go! then the program produces the output file /* 1 */ Mary had a little lamb /* 2 */ Whose Fleece was a white as snow. /* 3 */ And everywhere that Mary went, /* 4 */ The lamb was sure to go! The line numbers are enclosed in /* */ delimiters so the program can be used for numbering Java source files. Prompt the user for the input and output file names.

1 Answer

2 votes

#!/bin/bash

INPUT_FILE=$1

count=1

while IFS= read -r line; do

echo $count - $line

count=$(($count + 1))

done < $INPUT_FILE

User Alexander Berndt
by
5.1k points