Bash, grep between two lines with specified string

后端 未结 8 1970
栀梦
栀梦 2020-11-27 12:59

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

8条回答
  •  旧巷少年郎
    2020-11-27 13:28

    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
    

提交回复
热议问题