Bash, grep between two lines with specified string

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

    To make it more deterministic and not having to worry about size of file, use the wc -l and cut the output.

    grep -Awc -l test.txt|cut -d" " -f1 test1 test.txt | grep -Bwc -l test.txt|cut -d" " -f1 test2

    To make it easier to read, assign it to a variable first.

    fsize=wc -l test.txt|cut -d" " -f1; grep -A$fsize test1 test.txt | grep -B$fsize test2

提交回复
热议问题