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
| sed -e '1d;$d'
, that is remove first and last line – holroy Jun 26 '15 at 09:54sed -n -e '/pattern A/,/pattern D/d; p'
This says, "delete from pattern A to pattern D and print everything else". Unfortunately, this match is greedy. That means if patterns A and D appear more than once, you're going to match from the first pattern A to the last pattern D. I'm afraid Perl or Python are your friends if that's the case. – fbicknel Dec 19 '18 at 19:07sed -n -e "/$pattern1/,/$pattern2/ p" file
. see https://askubuntu.com/questions/76808/how-do-i-use-variables-in-a-sed-command – enharmonic Jan 28 '21 at 18:16