Search for multiple lines in large file

Hi,
I have a requirement to search for a string in a large log file along with few lines before and after the the string. The following script was sufficient to search such an entry.

STRING_TO_GREP="$1"
FILE_TO_GREP="$2"
NUMBER_OF_LINES_BEFORE=$3
NUMBER_OF_LINES_AFTER=$4
for i in `grep -n "${STRING_TO_GREP}" $FILE_TO_GREP|cut -d ':' -f1`
do
echo "Line Number $i"
echo "--------------"
i1=$(($i-NUMBER_OF_LINES_BEFORE))
i2=$(($i+NUMBER_OF_LINES_AFTER))
if [ ${i1} -le 0 ]; then
i1=1
fi
sed -n "${i1},${i2}p" $FILE_TO_GREP
done

But my problem is that I do not know the value of NUMBER_OF_LINES_BEFORE and NUMBER_OF_LINES_AFTER beforehand, but needs to be identified based on the occurence of another string S1 just before the occurence of STRING_TO_GREP.

For example, if the file F1.txt contains:
a
b
c
d
e
f
g
h
i
j
c

I want to retrieve the following text based on the grep for 'f' with 'c' as the start of text and 'i' as the end. Please note that I do not want to include the c given in the end of file.
c
d
e
f
g
h
i

Thanks & Regards,
Praveen

hi, maybe you can use the -A an d -B option of grep, i put and example :
texto= is a file for example, has 6lines
man grep:
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing -- between contiguous groups of
matches.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
Places a line containing -- between contiguous groups of
matches.

$more texto
line1
line2
line3
line4
line5
line6
$grep -A 2 -B 1 "line3" texto
line2
line3
line4
line5
$grep -A 1 -B 2 "line3" texto
line1
line2
line3
line4

Thanks guys. Unfortunately there is no -A and -B option in HP-UX. But I received another response about using awk 'c==2 && /i/{print;exit}/[cf]/{c=c?2:1}c' file, which seems to be a very good option. I will try to understand this and tweak for my requirement. Thanks.

Regards,
Praveen

Nop, that's why I delete my post :wink:
You can try

awk '/i/ && c==2{print;exit} /f/{c=2;for(i in a)print a;print}/c/{c=1}c==1{a[NR]=$0}c==2' file