Grep Command – How to Show Lines After Each Grep Match Until Another Specific Match

grep

I know that by using the "-A NUM" switch I can print specific number of trailing lines after each match. I am just wondering if it's possible to print trailing lines until a specific word is found after each match. e.g. When I search for "Word A" I want to see the line containing "Word A" and also the lines after it until the one containing "Word D".

context:

Word A
Word B
Word C
Word D
Word E
Word F

command:

grep -A10 'Word A'

I need this output:

Word A
Word B
Word C
Word D

Best Answer

It seems that you want to print lines between 'Word A' and 'Word D' (inclusive). I suggest you to use sed instead of grep. It lets you to edit a range of input stream which starts and ends with patterns you want. You should just tell sed to print all lines in range and no other lines:

sed -n -e '/Word A/,/Word D/ p' file
Related Question