Seeking for help in writing shell

Hi All,

I got a problem and stuck to filter a log file, called it as "sample.log". This "sample.log" file is being generated by "script A". The "sample.log" look like below:

:FORMATDATE_FORMATTIME:CmdArg->:SomeDoce:SomeFormatDocument:
:FORMATDATE_FORMATTIME:imprtcp succeeded.
:FORMATDATE_FORMATTIME:imprttyp completed.
:FORMATDATE_FORMATTIME:fdcode2 failed
::
::...........................
:FORMATDATE_FORMATTIME:CmdArg->:SomeDoce:SomeFormatDocument:
:FORMATDATE_FORMATTIME:imprtcp succeeded.
:FORMATDATE_FORMATTIME:imprttyp completed.
:FORMATDATE_FORMATTIME:DocCode = yadayada(00)
:FORMATDATE_FORMATTIME:Document type is yada.
:FORMATDATE_FORMATTIME:Document not paginated or not text.
:FORMATDATE_FORMATTIME:yada yada succeeded.
:FORMATDATE_FORMATTIME:yada yada succeeded, size=1248.
:FORMATDATE_FORMATTIME:nimport succeeded.

What i try to do is,Since the "sample.log" is actively growing up. i need to

1) Grep the the value failed and also all the required data. i should get like below:-

:FORMATDATE_FORMATTIME:CmdArg->:SomeDoce:SomeFormatDocument:
:FORMATDATE_FORMATTIME:imprtcp succeeded.
:FORMATDATE_FORMATTIME:imprttyp completed.
 :FORMATDATE_FORMATTIME:fdcode2 failed

2) and email it to the me as an alert.

P/S this script will run in cron to actively monitor the "failed" data.
May be somebody cay help me on figure it out how can i do this.

Currently what i have is:

1) tail -f sample.log | grep failed >> samplelog.failed
## but i stuck at here because tail -f cannot output to other files, i tried but failed, no output has been generate.

2)
test=$(cat samplelog.failed | cut -d: -f-2)
cat sample.log | grep $test: | /usr/bin/mailx -s "Subject" to@somebody

Thanks for your helping in here.

Try this.

head test.txt | egrep "failed" | while read line
do
   print - $line | read word1 word2 word3
   if [[ $word2 = "failed" ]]
   then
      email command
   fi
done