look for two consecutive lines in all text files

How to get (a list of) all the text files in the current directory and subdirectories which has the following two consecutive lines:
ctrl_end_date=2009
ctrl_process=EXPIRED
OR
ctrl_end_date=2010
ctrl_process=EXPIRED

i.e.
(ctrl_end_date=2009 OR ctrl_end_date=2010)
AND
ctrl_process=EXPIRED

---------- Post updated at 11:07 AM ---------- Previous update was at 10:41 AM ----------

a related question:
How to get (a list of) all the text files in the current directory and subdirectories which has the following two consecutive lines:
ctrl_end_date=nnnnnnnn
ctrl_process=EXPIRED

where nnnnnnnn is <= 20100902
e.g.
nnnnnnnn is a date string
20070614 and 20100503 meet the criteria
but 20101102 does not.

For your first question you can use

awk -F\= '{if(x&&$0=="ctrl_process=EXPIRED"){a[FILENAME]++}}{x=($NF>2008&&$NF<2011&&$1=="ctrl_end_date")?1:0}END{for(i in a){print i}}' *

---------- Post updated at 12:25 PM ---------- Previous update was at 12:17 PM ----------

This is for your second question.

awk -F\= '{if(x&&$0=="ctrl_process=EXPIRED"){a[FILENAME]++}}{x=($NF<=20100903&&$1=="ctrl_end_date")?1:0}END{for(i in a){print i}}' *

not a final solution but you can start from here :wink:
Please use [code] tags when you post code or data sample

grep -R -e 'ctrl_end_date=2009' -e 'ctrl_end_date=2010' --after-context=1 | grep -e 'ctrl_process=EXPIRED'|awk {print $1}

Please help to fix the following error.
Thanks.

$ awk -F\= '{if(x&&$0=="ctrl_process=EXPIRED"){a[FILENAME]++}}{x=($NF>2008&&$NF<2011&&$1=="ctrl_end
_date")?1:0}END{for(i in a){print i}}' *

awk: cmd. line:1: fatal: cannot open file `testing' for reading (Is a directory)

Everithing should be in one line

awk -F\= '{if(x&&$0=="ctrl_process=EXPIRED"){a[FILENAME]++}}{x=($NF>2008&&$NF<2011&&$1=="ctrl_end_date")?1:0}END{for(i in a){print i}}' *
awk -F = '/^ctrl_end_date/&& $NF<=20100902 {print;getline;print;next}' infile

What if the next line is not

ctrl_process=EXPIRED

Anyway the OP is looking for the filename(s).