Search for text between two patterns with multiple lines in between

后端 未结 3 1945
[愿得一人]
[愿得一人] 2021-01-28 15:15

I have a simple question. I have a file containing:

more random text

*foo*
there
is 
random
text
here
*foo*

foo
even
more
random
text
here
foo
more random text         


        
3条回答
  •  难免孤独
    2021-01-28 16:01

    $ awk '/foo/{++c;next} c==1' file
    there
    is
    random
    text
    here
    
    $ awk '/foo/{++c;next} c==3' file
    even
    more
    random
    text
    here
    

    or with GNU awk for multi-char RS you COULD do:

    $ awk -v RS='(^|\n)[^\n]*foo[^\n]*(\n|$)' 'NR==2' file
    there
    is
    random
    text
    here
    
    $ awk -v RS='(^|\n)[^\n]*foo[^\n]*(\n|$)' 'NR==4' file
    even
    more
    random
    text
    here
    

    See https://stackoverflow.com/a/17914105/1745001 for other ways of printing after a condition is true.

提交回复
热议问题