#!/bin/bash [[ $# -ne 4 ]] && { echo "Usage: $0 pre_patt_numb post_patt_numb patt filename" echo "Usage: $0 2 2 mail /var/adm/messages" exit 1 } pre=$1 post=$2 patt="$3" fn=$4 [ -f $fn ] || { echo "$0: $fn does not exist or insufficent permission" exit 1 } egrep -in "$patt" $fn | awk -F':' ' { print $1 } ' | while read numb do this_pre=`expr $numb - $pre` this_post=`expr $numb + $post` echo "#_________________ Output of $fn from line ${this_pre} to ${this_post} __________#" sed -n ${this_pre},${this_post}p $fn done exit 0