Write a shell script myFile which will output the total number of lines in the f
ID: 3915832 • Letter: W
Question
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). Use wc to count number of lines, use sed to read/print the lines. Use > to store them in newFile.
$myFile test 100 150
For example, in the command above, 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.
This is currently my script but it is not working.
File EditView Search Terminal Help #!/bin/bash wc L $1 Sa-S(wc 1 $11sed 's/*//i') 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 "myFile" 12L, 2110 12,2Explanation / Answer
ch= `cat $1 | wc -c`
lin= `grep -ch "." $1`
echo Number of lines is $lin
if [ $lin -lt $2 ]
then
echo Lines do not exist
elif [ $a -lt $3 ]
then
echo lines do not exist
else
sed -n $2,$3p $1 > newfile.txt
cat newfile.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.