As unSpawn said, there is a command line option to the grep program which will help you do this.
You didn't state what programming language you are using, and you didn't post what code you have so far, so it's very hard to give you more precise help.
If you are writing a shell script, you don't need to read through the file with your own loop - it is more normal (and usually more efficient) to use grep or one of the other ubiquitous text processing programs (like sed, awk, tr, cut, paste, fold, head, tail etc). What if there is more than one line in the input file which has the search pattern in it? Do you want to do something for all the following lines, or just the first one? Perhaps this will help:
Code:
$ cat data
data#1 First line
data#2 Second line
data#3 Third line
data#4 Fourth line
$ grep -A 1 -m 1 "data#1" data |tail -1
data#2 Second line
You can assign the output of one program to a variable like this:
Code:
myvariablename=$(some command here)