1

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
7
  • please edit your question to add sample input and your desired output from that..
    – heemayl
    Feb 9, 2016 at 6:33
  • Some example input and output would really help; your question is indecipherable so far Feb 9, 2016 at 6:53
  • @kos I need 3 lines before my start word pattern
    – Mansour
    Feb 9, 2016 at 8:49
  • 2
    Do grep -Pzo '(?s)\n\K([^\n]*\n){3}[^\n]*command[^\n]*\n.*?\n[^\n]*ideal[^\n]*(?=\n)' file.txt ..if the question is reopened, let me know..i will put it as an answer with details..
    – heemayl
    Feb 9, 2016 at 9:17
  • 1
    If my or kos's answer helped, please select it as accepted by clicking the tick mark on the left of the answer so that this issue can be marked as solved..
    – heemayl
    Feb 10, 2016 at 4:12

2 Answers 2

2

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
0
1

Another way using Perl:

perl -ne 'BEGIN{ my @buffer } unshift(@buffer, $_); splice(@buffer, 4); if(/\bcommand\b/) { print(reverse(@buffer)); while(<>) { print(); /\bideal\b/ && exit } exit }' file
  • n: causes Perl to assume the following loop around your program, which makes it iterate over filename arguments somewhat like sed -n or awk:

    LINE:
      while (<>) {
          ...             # your program goes here
      }
    
  • -e: may be used to enter one line of program.
  • BEGIN{ my @buffer } unshift(@buffer, $_); splice(@buffer, 4); if(/\bcommand\b/) { print(reverse(@buffer)); while(<>) { print(); /\bideal\b/ && exit } exit }: inserts the current line at the start of a buffer and truncates the buffer to a length of 4; if the current line contains the word command, prints the buffer in reverse order and iterates through all the remaining lines printing them; if a printed line contained ideal or the end of file had been reached, exits.
% cat file
my name is
Mansour
and I confuse to
use sed
or grep for
piping my command output
to get 
my ideal output
% perl -ne 'BEGIN{ my @buffer } unshift(@buffer, $_); splice(@buffer, 4); if(/\bcommand\b/) { print(reverse(@buffer)); while(<>) { print(); /\bideal\b/ && exit } exit }' file
and I confuse to
use sed
or grep for
piping my command output
to get 
my ideal output
%
4
  • You love perl..don't you? :)
    – heemayl
    Feb 10, 2016 at 4:12
  • @heemayl Sure! When the other tools don't suffice or grep has been taken already... ;) (sometimes I prefer it to AWK because it's easier to manipulate arrays)
    – kos
    Feb 10, 2016 at 7:49
  • Yeah..awk knows its job very well..may be someday i will start learning it from you :)
    – heemayl
    Feb 10, 2016 at 7:49
  • @heemayl You could have better teachers, trust me :) Yes AWK is very useful when processing something which has a natural records/fields structure, knowing it and using it when appropriate in place of sed / grep is like stopping to cut paper using the knife and starting to do that using the scissors, if you know what I mean. :)
    – kos
    Feb 10, 2016 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.