3) Write a shell script myFile which will output the total number of lines in th
ID: 3915791 • Letter: 3
Question
3) Write a shell script myFile which will output the total number of lines in the file whose name will be given by the user. Also, it will read certain lines from the file and put them in a new file called (newFile). For example, in the command bellow test is the name of the file given by the user 100 is the starting line number and 150 is the ending line number. Your code will output the lines from 100 150 in the file "newFile". If the numbers of lines in the file are less than 100 or 150 then the program will output a message saying; "Lines do not exist in file". You can assume that user will give a file name which exists in the current directory. Hints: Use wc to count number of lines, use sed to read/print the lines. Use to store them in newFile ?myFile test 100 150Explanation / Answer
#!/bin/bash
wc -l $1
$a=$(wc -l $1|sed 's/ .*//')
if ["$a" -lt "$2"]
then
echo "Lines do not exist"
elif ["$a" -lt "$3"]
then
echo "Lines do not exist"
else
sed -n '$2,$3'p $1>newfile.txt
cat newfile.txt
fi
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.