how can I find lines between two words with 3 lines before START_WORD in sed or grep or …
for example output from a command is:
my name is
Mansour
and I confuse to
use sed
or grep for
piping my command output
to get
my ideal output
and I want my out after piping the command with sed or grep on START_WORD=command and END_WORD=ideal:
and I confuse to
use sed
or grep for
piping my command output
to get
my ideal output
Best Answer
You can do:
-P
enables Perl Compatible Regex,-z
makes the input data separated on ASCII NUL rather than usual newline,-o
lets up take only matched portion(?s)
is DOTALL modifier, this enables us to match newline (\n
) with.
token\n\K([^\n]*\n){3}
matches\n
at first and then discards the match\K
,([^\n]*\n){3}
matches three lines before line containingcommand
[^\n]*command[^\n]*\n.*?\n[^\n]*ideal[^\n]*(?=\n)
matches all lines starting from line havingcommand
till the line containingideal
Example: