This extracts all the lines after SUBDIRS but before OTHERS excluding lines that start with # or are empty:
$ awk '/OTHERS/{f=0} f && /./ && !/^#/{print} /SUBDIRS/{f=1}' file
text1 \
text2 \
text3 \
text4 \
How it works
This program uses one variable, f: f is 1 when we are in the desired range of lines and 0 elsewhere.
/OTHERS/{f=0}
If we have reached OTHERS, then set f back to zero.
f && /./ && !/^#/{print}
If f is nonzero (f) and the line is not blank (/./) and the line does not start with # (!/^#/), then print this line.
/SUBDIRS/{f=1}
If we have reached the line containing SUBDIRS, then set f to 1.
Slightly briefer form
Because print is the default action when no action is specified, we can omit it from the script:
$ awk '/OTHERS/{f=0} f && /./ && !/^#/; /SUBDIRS/{f=1}' file
text1 \
text2 \
text3 \
text4 \
Alternative output format
This removes the trailing \ and combines all the output to one line:
$ awk '/OTHERS/{f=0} $NF=="\\"{$NF=""} f && /./ && !/^#/{a=a" "$0} /SUBDIRS/{f=1} END{print a}' file
text1 text2 text3 text4
grep -ow 'text[0-9]'could do the job for the example (assuming GNUgrep). If you want the file list associated withSUBDIRS, then you have to work harder — this is what the answers so far have assumed. What should the output format be? One word per line without backslashes? Does the code need to handle all the niceties ofmakesyntax? How will you invoke the command? What should happen if the name specified is not found in the file?