I need to create a shell script which will output certain lines in a file. For example ./line file1 6 11 will output lines 6 to 11 of the file. I am not good at shell script, I am a beginner. I have tried to write this script but just cannot. If anybody can help by giving me a website similar to this problem, please send it thanks.

I need to create a shell script which will output certain lines in a file. For example ./line file1 6 11 will output lines 6 to 11 of the file. I am not good at shell script, I am a beginner. I have tried to write this script but just cannot. If anybody can help by giving me a website similar to this problem, please send it thanks.

Try using the while loop and command parameters

#! /bin/sh

echo -n "enter filename->"
read myfile

while read f
do
	echo "line $((++cntr)) is->$f"
done < $myfile

You should be able to figure the rest out..

sed -n '6,11p' file1
sed -n '6,11p' file1

Show-off

Thank you so much!

I thought it was working but it is not....I do not understand whats the problem...

Try using the while loop and command parameters

#! /bin/sh

echo -n "enter filename->"
read myfile

while read f
do
	echo "line $((++cntr)) is->$f"
done < $myfile

You should be able to figure the rest out..

what is the ++cntr doing??

I need to write the code on pico then on the file when I type lets say 6-11 it should show lines 6-11 of the file

Aia posted the answer for you..use

sed -n '6, 11p' filename

Aia posted the answer for you..use

sed -n '6, 11p' filename

but the thing is I have to write a shell script to do it... I have to write the script in pico...then on the terminal...Say I type 5 10 then lines 5 10 of the file should be shown.

# Shell script to print contains of file from given line no to next
# given numberlines
#

#
# Print error / diagnostic for user if no arg's given
#
if [ $# -eq 0 ]
then
echo "$0:Error command arguments missing!"
echo "Usage: $0 start_line uptoline filename"
echo "Where start_line is line number from which you would like to print file"
echo "uptoline is line number upto which would like to print"
echo "For eg. $0 5 5 myfile"
echo "Here from myfile total 5 lines printed starting from line no. 5 to"
echo "line no 10."
exit 1
fi

#
# Look for sufficent arg's
#

if [ $# -eq 3 ]; then
if [ -e $3 ]; then
tail +$1 $3 | head -n$2
else
echo "$0: Error opening file $3"
exit 2
fi
else
echo "Missing arguments!"
fi

if you look at the code above...when I type lets say 5 10... it shows certain lines of the file but not lines 5-10.. do you know whats wrong with the code above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.