Bash, grep between two lines with specified string

后端 未结 8 1958
栀梦
栀梦 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:29

    You can do something like this too. Lets say you this file test.txt with content:

    a43
    test1
    abc
    cvb
    bnm
    test2
    kfo
    

    You can do

    cat test.txt | grep -A10 test1 | grep -B10 test2

    where -A is to get you n lines after your match in the file and -B is to give you n lines before the match. You just have to make sure that n > number of expected lines between test1 and test2. Or you can give it large enough to reach EOF.

    Result:

    test1
    abc
    cvb
    bnm
    test2
    

提交回复
热议问题