search text file in file if this file contains necessary text (awk,grep)

Hello friends!
Help me pls to write correct awk and grep statements for my task:

I have got files with name filename.txt
It has such structure:
Start of file
FROM: address@domen.com (12...890) abc
DATE: 11/23/2009 on Std
SUBJECT: any subject
End of file

So, I must check,
if this file DOESN'T CONTAIN mail address = mike@domen.com,
I must save text address@domen.com(after "FROM: " and before first white-space after mail-address) to variable $Address and 11/23/2009 (after "DATE: "and before first white-space after date) to variable $Date.

So, I need to write bash script like this:

if [ ! grep('mike@domen.com',textfile.txt) ] then
 Address=awk '/^FROM: /,/^ /' textfile.txt
 Date=awk '/^DATE: /,/^ /' textfile.txt
 echo $Address $Date
else
 echo "sorry, but this file contains mike@domen.com text :("
fi

P.S. This code, of course, doesn't work. Pls help me to fix it.

Try:

if [ -z "$(grep mike@domen.com filename.txt)" ]; then
  Address=$(awk '/^FROM:/{print $2}' filename.txt)
  Date=$(awk '/^DATE:/{print $2}' filename.txt)
  echo $Address $Date
else
  echo "sorry, but this file contains mike@domen.com text :("
fi

Thank you! :slight_smile:

---------- Post updated 04-20-11 at 02:38 AM ---------- Previous update was 04-19-11 at 02:29 PM ----------

Sorry, but my file has some different format:
Start of file
FROM: "some name "<name@server.com>
DATE: Wed, 18 Apr 2011 02:53:12 +0400
SUBJECT: any subject
End of file

I need to get name@server.com in $Address and
18 Apr 2011 02:53:12 converting in 18.04.2011 02:52:12 in $Date

Please, change these awk statements to the correct:

Address=$(awk '/^FROM:/{print $2}' filename.txt)
  Date=$(awk '/^DATE:/{print $2}' filename.txt)

---------- Post updated at 08:34 AM ---------- Previous update was at 02:38 AM ----------

eval $(nawk -f candy.awk filename.txt)
echo $Address
echo $Date

candy.awk:

BEGIN {
 q=sprintf("%c", 039)
 mon="JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC"
   monN=split(mon, monA, "|");
   for(i=1; i<=monN; i++) {
     monA[monA]=i;
     delete monA;
   }
}
/^FROM:/ { n=split($0,a,"[<>]"); print "Address=" a[n-1]}
/^DATE:/ {printf("Date=%c%.02d.%.02d.%d %s%c\n",q, $3,monA[toupper($4)],$5,$6,q)}

vgersh99

Thank you!

but can I use your script in this one:

if [ ! grep('mike@domen.com',textfile.txt) ] then
 Address=awk '/^FROM: /,/^ /' textfile.txt
 Date=awk '/^DATE: /,/^ /' textfile.txt
 echo $Address $Date
else
 echo "sorry, but this file contains mike@domen.com text :("
fi  

??
in one script-file