Print contents of a file using Shell Script

By: Vivek G Viewed: 153440 times  Printer Friendly Format    


This shell script prints contents of file from given line number to next given number of lines. For e.g. If we called this script as printfile and run as $ printfile 5 5 myfile , Here print contents of 'myfile' file from line number 5 to next 5 line of that file.
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



Most Viewed Articles (in Linux )

Latest Articles (in Linux)

smskannel SMS gateway run in background

Running jar files in background in ssh window

Could not open '': No such file or directory at lib/ExtUtils/MM_Unix.pm line 2697

Can't locate ExtUtils/MakeMaker.pm in @INC ...

bash: make: command not found

apxs: command not found

bash: svn: command not found

make: Nothing to be done for `all'.

How to burn your CD / DVD ISO image using Media Creator (Adaptec/Roxio) on Windows

How to burn your CD / DVD ISO image using Nero Burning ROM (Ahead Software) on Windows

How to burn your CD / DVD ISO image using Nero Express (Ahead Software) on Windows

How to burn your CD / DVD ISO image using NISO Recorder V2 Power Toy on Windows

How to burn your CD / DVD ISO image using k3b on CentOS

Compiling and Installing software from source in Linux

Installing RPM's using the Redhat Package Manager in Linux

Comment on this tutorial