How can I set the grep after context to be “until the next blank line”?

后端 未结 4 1090
伪装坚强ぢ
伪装坚强ぢ 2021-01-30 13:31

With grep I know how to set the context to a fixed number of lines. Is it possible to show a context based on an arbitrary string condition, like set after-context to \"until th

4条回答
  •  醉梦人生
    2021-01-30 13:44

    Personally I like the answer from @William Pursell as the before context is often useful (eg when grepping for things in ini files). If you actually want only the after context with awk you can do this:

    $ cat demo.ini 
    [foo]
    aaa = 1
    bbb = 2
    ccc = 3
    
    [bar]
    eee = 8
    fff = 0
    ggg = 1
    
    [baz]
    xxx = 1
    yyy = 0
    zzz = 2
    $ awk '/bar/,/^$/' demo.ini 
    [bar]
    eee = 8
    fff = 0
    ggg = 1
    
    $ awk '/fff/,/^$/' demo.ini 
    fff = 0
    ggg = 1
    
    $
    

    Compare with the RS= version:

    $ awk '/bar/' RS= demo.ini 
    [bar]
    eee = 8
    fff = 0
    ggg = 1
    $ awk '/fff/' RS= demo.ini 
    [bar]
    eee = 8
    fff = 0
    ggg = 1
    

提交回复
热议问题