Ubuntu – Text between 2 words with some lines before patterns

command linetext processing

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:

grep -Pzo '(?s)\n\K([^\n]*\n){3}[^\n]*command[^\n]*\n.*?\n[^\n]*ideal[^\n]*(?=\n)' file.txt
  • -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 containing command

  • [^\n]*command[^\n]*\n.*?\n[^\n]*ideal[^\n]*(?=\n) matches all lines starting from line having command till the line containing ideal

Example:

% cat file.txt
my name is
Mansour
and I confuse to
use sed
or grep for
piping my command output
to get 
my ideal output

% grep -Pzo '(?s)\n\K([^\n]*\n){3}[^\n]*command[^\n]*\n.*?\n[^\n]*ideal[^\n]*(?=\n)' file.txt
and I confuse to
use sed
or grep for
piping my command output
to get 
my ideal output