Need help writing a small script

Hi I am trying to do the following:

run a command that looks at a binary file and formats it
find the first line that has what I am looking for (there will be multiple)
print the name of the file that has it

So I am running this but ti's not working

ls -ltr *20080612* | while read line
do
convertfile.ksh $line | grep "2007"
echo $line
done

Do I need an IF statement somewhere?

Thanks!

ls -ltr *20080612* | while read binary; do
convertfile.ksh "$binary" | grep -m1 -q "2007" && echo "$binary contains 2007"
done

short for ... | grep -q "2007"; if [ $? -eq 0 ]; then echo "$binary contains 2007"; fi also speed up with "-m1" (or | head -1) because finding one instance is enough.

thank you for the reply!

can you expand on this? please bear with me I'm just learning:

grep -q "2007"; if [ $? -eq 0 ];

-q is option for what again?

[ $? -eq 0 ]; what does this break down to?

The man pages are your friends. Try

man grep

Specifically, in this case it means to be quiet. No output will be produced- the default is to print to the screen what grep finds. In your case, you don't want to see what it found, you just want to know what it found, and report it. The -q option is perfect for that.

Regardless, grep exits with a code. If it finds something, it exits with code 0 (which perhaps paradoxically means "success" in UNIX shell-land). That number is put into the "$?" variable. Also, the [ is the shell command "test". So

[ $? -eq 0 ]

means "Test the $? variable and see if it's equal to 0". If so, the test command will exit with code 0 (success). The "if" command checks the exit code of the command it is given- in this instance, the "test" command. So if the test is successful, then do the "then" clause.
-mschwage