Shell Script to cut n Lines Starting with Position m Without Using head or tail Commands

shell script is a computer program designed to be run by the Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script that sets up the environment, runs the program, and does any necessary cleanup, logging, etc. is called a wrapper.

In this post, we will write a shell script to cut n lines starting with position m from a file without using head or tail commands.

INPUT:
line 1: filename
line 2: number of lines (n)
line 3: starting position (m)

OUTPUT:
lines

The following is the shell script to cut n lines starting with position m without using head or tail commands:

echo enter fiename
read file
echo enter no of line
read n
echo enter starting position
read m
t=$(expr $n + $m)
e=1
exec < $file
while read line
do
if [ $e -ge $m -a $e -lt $t ]
then
echo $line
fi
e=$(expr $e + 1) 
done

OUTPUT:

$ enter fiename
$ a
$ enter no of line
$ 3
$ enter starting position
$ 2
$ line 2
$ line 3
$ line 4

Let us know in the comments if you are having any questions regarding this shell script.

And if you found this post helpful, then please help us by sharing this post with your friends. Thank You

Default image
Jazib
Android Developer | Competitive Programmer

Leave a Reply