Wednesday, 24 April 2013

Unix Shell Script: Print lines between two given lines in a file


#!/bin/bash

# This script will print lines between start line and end line of testfile. 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 field can be provided from both of start line and end line. Then value to variable line can be 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

No comments:

Post a Comment