regexbashgrep

Bash, grep between two lines with specified string


Example:

a43
test1
abc
cvb
bnm
test2
kfo

I need all lines between test1 and test2. Normal grep does not work in this case. Do you have any propositions?


Solution

  • Print from test1 to test2 (Trigger lines included)

    awk '/test1/{f=1} /test2/{f=0;print} f'
    awk '/test1/{f=1} f; /test2/{f=0}' 
    awk '/test1/,/test2/'
    

    test1
    abc
    cvb
    bnm
    test2
    

    Prints data between test1 to test2 (Trigger lines excluded)

    awk '/test1/{f=1;next} /test2/{f=0} f' 
    awk '/test2/{f=0} f; /test1/{f=1}' 
    

    abc
    cvb
    bnm