Extract if pattern matches

Hi GhostDog,

I am not sure if you understand my requirement. Any way the below code can work. It's just that i would only want to print the very last block if the condition fits. The code in blue need some adjustment has the assumption that the block that matches the pattern will occur twice, however we can't make that assumption since the block can appear more than twice in the input
The block i am refering to is the below in purple.
Can you help ?

** abc ccc cc cc cc cc 2007
0001 k= 1
wwwwww
0002 k= 1
0003 k= 1

FNR != NR && $2 ~ line && CODE != "XX" {
flag=2;
occur++;
if (occur > 1) print;
}
flag==2 && occur > 1 && $2 == "k=" { print }

FNR==NR&&/^\*\*/{line=$2; CODE = substr ($2,1,2); next}

FNR != NR && $0 ~ line {
      print 
      flag=1
     }
     flag == 1 && $0 ~ /^\*\*/ && CODE == "XX"{ 
       if($2 !~ line) flag=0
     }
     flag == 1 && $2 == "k="{print}


FNR != NR && $2 ~ line && CODE != "XX"  {
      flag=2;
      occur++;
      if (occur > 1)  print;
     }
      flag==2 && occur > 1 && $2 == "k=" { print }