TECH SOLUTIONS

Click here to edit subtitle

Forums

Post Reply
Forum Home > Unix Learnings > Print lines between two given lines in a file

Sourav Gulati
Site Owner
Posts: 83

#!/bin/bash

#Author : Sourav Gulati

# This script will print lines between start line and end line oftestfile. Start line and end line are to be provided at run time.

# In log file , lines are usually very lengthy . Instead of providing whole line as input , a particular fileld can be provided from both of start line and end line. Then value to variable line canbe assigned as   

#line=`cat testfile | sed -n "$i p" | awk '{print $field_number}' `

print=no

echo "Enter start line"

read startline

echo " Enter end line"

read endline

echo ""

#specify full path of the file

n=`cat testfile | wc -l`

i=1

while [ $i -le $n ]

do

#specify full path of the file

line=`cat testfile | sed -n "$i p" `

if [ $line = $endline ]

then

print=no

fi

if [ $print = yes ]

then

# specify full path of the file

cat testfile | sed -n "$i p"

fi

if [ $line = $startline ]

then

print=yes

fi

i=$(( $i + 1 ))

done

 

 

 

 

 

--


August 21, 2012 at 8:03 AM Flag Quote & Reply

You must login to post.