search backwards relative to a string

Hi,

I have to search for first occurenceof string str1 in a file(>5GB).

Now, after I have that , I have to search backwards from that offset till I get another string str2. I should also be able to get the
new string str2's offset.

Similarly, I look for last occurence of str1 and then have to search forward from there for str2 and get its offset .

First occurence :--
grep str1 file | head -1

Last occurence :--
grep str1 file | tail -1

But, I dont know how to do relative search in backward & forward directions, so that I have their offsets ..??

Please help...

finder

cat -n will display a file with line number
tac will display a file in reverse order

These two commands, and in combination with other commands, should be able to get you on your way. Try to write something, then come back if still issues.