I have a directory with thousands of file with following repetitive pattern containing hundreds of sections -################ Section 1###############some textmore textsome more textsome textmore textsome more text    ################ Section 2###############some textmore textsome more textinteresting-patternsome textmore textsome more text    ################ Section 3###############some textmore textsome more textsome textmore textsome more textWhat i need to do is figure out a way to extract the entire SECTION where the interesting-pattern exists.I have tried doing a grep -iEr 'interesting-pattern' with -A and -B flags but that doesnt work because in each file there could be different number of lines in the section before and after the intersting pattern.What is the best way to do this? 
print varying number of lines before and after a pattern
grep
This is not a job for grep , but for a better tool like awk.  The easy workaround is to use gnu awk with a customized record separator RS like Section. Using word Section to separate lines, whatever is between words Section 1 and Section 2 will be considered as one line for awk.Same for Section 2 - Section 3, etc.  Now you only need to print the correct line = the line that includes the interesting-pattern.$ awk -v RS=# Section  '/interesting-pattern/{print RT $0}' file1# Section 2###############some textmore textsome more textinteresting-patternsome textmore textsome more text    ###############Since gnu awk can accept regex in RS (Record Separator) you could also apply something more complicated in RS like this :$ awk -v RS=###############\n# Section  '/interesting-pattern/{print RT $0}'################ Section 2###############some textmore textsome more textinteresting-patternsome textmore textsome more text    PS: {print RT instructs awk to print the currently used Record Separator