I am trying to use the awk range pattern to find all SQL select statements in a group of files, inspired by this stackoverflow answer.

From the awk manual:


  The pattern1, pattern2 form of an expression is called a range pattern.  It matches all input records starting with a record that matches pattern1, and continuing until a record that matches  pattern2,  inclusive.


My initial attempt was 

awk '/select/,/from/' *


where * in this case just represents a large number of varied files.

This returned several false hits on HTML select tags, so I refined my command to

awk '/[^&lt;]select[^&gt;]/,/from/' *


which seems to have eliminated most of those hits.

However, I still get some false hits from occurrences of the word "select" in a comment, and those hits produce very many lines of noise each before they eventually hit a "from" or the end of the file.
What I would like is for the range pattern not to register a match if there are more than, say, 10 lines between the "select" and the "from".

My question is: Can I make the range pattern fail to match if the number of lines between the match of pattern1 and the match of pattern2 exceeds a given threshold, and if so, how?


Question: Etiquette for closing your own questions
Answer: I definitely agree with Euro Micelli; closing the question seems very drastic.

However I'd say an important thing to maintain is to select an answer as the accepted answer to indicate which solution was most appropriate to you; and to vote up good answers and perhaps vote down bad answers. I've seen many questions where there are several very good answers provided but it seems that the questioner has taken the answer and lost interest. That may well make people think an ideal answer hasn't been determined when it has. I think it's vitally important to show your gratitude for good answers and perhaps scorn for bad so votes act as a more accurate measure of answer quality.

Apologies if I have veered slightly off-topic here; however I think this point is related and rather important.