Using Raku (formerly known as Perl_6)
~$ raku -ne '.put if / Word \h A / fff^ / Word \h D /;' file
Raku is a programming language in the Perl-family. It's an "operator-rich" language that features a powerful Regex engine. Above, the -ne
non-autoprinting linewise flags are used, in conjunction with Raku's sed-like fff
"Flip-flop" operator.
Raku includes various 'flavors' of its sed-like fff
infix operator, including fff^
, ^fff
and even ^fff^
. While each Regex is recognized, the ^
caret indicates that recognized line should be dropped from the output:
Sample Input:
Word A
Word B
Word C
Word D
Word E
Word F
Word G
Word A
Word H
Word I
Word D
Word J
Word A
Word K
Word D
Word L
Word M
Word A
Word D
Sample Output:
Word A
Word B
Word C
Word A
Word H
Word I
Word A
Word K
Word A
The above code solves the OP's test case. But what if the /start/
and /stop/
Regexes are actually on the same line? For that problem you could try Raku's awk-like ff
operator:
~$ echo 'AB\nCD\nEF' | raku -ne 'say $_ if /A/ ff /B/;'
AB
~$ echo 'AB\nCD\nEF' | raku -ne 'say $_ if /A/ ff /C/;'
AB
CD
As compared to Raku's sed-like fff
operator:
~$ echo 'AB\nCD\nEF' | raku -ne 'say $_ if /A/ fff /B/;'
AB
CD
EF
~$ echo 'AB\nCD\nEF' | raku -ne 'say $_ if /A/ fff /C/;'
AB
CD
https://docs.raku.org/routine/fff
https://docs.raku.org/routine/ff
https://raku.org
Word D
s afterWord A
- stop at the first or the last one? What if there'sWord A
but noWord D
- print to end of file or not?Word D
withoutWord A
- print from start of file?OtherWord A
- should that matchWord A
? IfWord D
appears mid-line should the part of the line before it be printed? What if both exist on the same line? etc., etc.....