Extract lines between 2 tokens in a text file using bash

后端 未结 7 758
死守一世寂寞
死守一世寂寞 2020-12-05 07:26

i have a text file which looks like this:

random useless text 
 
para1 
para2 
para3 
 
random us         


        
7条回答
  •  南笙
    南笙 (楼主)
    2020-12-05 08:21

    No need for head and tail or grep or to read the file multiple times:

    sed -n '//{:a;n;//b;p;ba}' inputfile
    

    Explanation:

    • -n - don't do an implicit print
    • //{ - if the starting marker is found, then
      • :a - label "a"
        • n - read the next line
        • //q - if it's the ending marker, quit
        • p - otherwise, print the line
      • ba - branch to label "a"
    • } end if

提交回复
热议问题