Search multiple strings on a file and copy the string next to it

I tried awk for this, but failed <or my code is not correct? I dont know>. Can anyone help me on this?

---------- Post updated at 08:34 PM ---------- Previous update was at 08:29 PM ----------

my working file looks like this:

<empty>
<empty>
<empty>
NAME :ABC        AGE :15
GENDER :MALE
<other strings> 
<other strings>   
NAME :DEF       AGE :18
GENDER :FEMALE
<other strings> 
<other strings>   
NAME :GHI       AGE :5
GENDER :FEMALE 
<other strings> 
<other strings>   

output file should be:

ABC|15|MALE
DEF|18|FEMALE
GHI|5|FEMALE

i used this code:

awk -n 'data.awk > output

data.awk looks like this

{
	if (substr($1,1,4) == "NAME")
         {  
 	   name = substr($2,2) ; age = substr($4,2)
	   }
	if (substr($1,1,6) == "GENDER")
         {  
 	   gender = substr($2,2)
	   }
print name"|"age"|"gender
}

but it outputs like this:

||
||
ABC|15|MALE
ABC|15|MALE
ABC|15|MALE
ABC|15|MALE
DEF|18|FEMALE
DEF|18|FEMALE
DEF|18|FEMALE
DEF|18|FEMALE
GHI|5|FEMALE
GHI|5|FEMALE
GHI|5|FEMALE
GHI|5|FEMALE
||
||

what could be the problem?

BEGIN {
  FS="[ :]"
  OFS="|"
}
$1 == "NAME" { name =$3; age=$NF}
$1 == "GENDER" { print name, age, $NF}

Please start using code tags - it'll improve the chances of someone actually looking at your posts.

Thanks a lot! Il keep ur advice in mind

---------- Post updated 06-27-09 at 12:03 PM ---------- Previous update was 06-26-09 at 08:49 PM ----------

Hi! What do u mean by $NF? Tnx

awk '
/NAME/{
 for(i=2;i<=NF;i+=2){
   gsub(":","",$i)
   printf "%s|",$i
 }
}
/GENDER/{gsub(/GENDER.*:/,"");print}
' file

i suggest you start reading up on gawk to understand its basics.

@ ghostdog: how about 'age'? Im sorry, im new in unix. There are so much for me to learn. Can you do me a favor of explaining the code above? Thank u so much

why don't you run it and see ?

nawk 'BEGIN{
FS="[ :]"
}
/NAME/{
tmp=sprintf("%s|%s",$3,$NF)
getline
tmp=sprintf("%s|%s",tmp,$3)
print tmp
}' yourfile

perl:

$/="NAME";
while(<DATA>){
	print "$1|$2|$3\n" if /:([^ ]+)\s+AGE\s*:([^ \n]+).*GENDER\s*:([^ \n]+)/s;
}
__DATA__
<empty>
<empty>
<empty>
NAME :ABC        AGE :15
GENDER :MALE
<other strings>
<other strings>
NAME :DEF       AGE :18
GENDER :FEMALE
<other strings>
<other strings>
NAME :GHI       AGE :5
GENDER :FEMALE
<other strings>
<other strings>