remove all lines from a text file starting at first empty line

前端 未结 5 1031
一生所求
一生所求 2020-12-18 16:17

What is the best way to remove all lines from a text file starting at first empty line in Bash? External tools (awk, sed...) can be used!

Example

1:          


        
5条回答
  •  -上瘾入骨i
    2020-12-18 16:43

    Another awk would be:

    awk -vRS= '1;{exit}' file
    

    By setting the record separator RS to be an empty string, we define the records as paragraphs separated by a sequence of empty lines. It is now easily to adapt this to select the nth block as:

    awk -vRS= '(FNR==n){print;exit}' file
    

    There is a problem with this method when processing files with a DOS line-ending (CRLF). There will be no empty lines as there will always be a CR in the line. But this problem applies to all presented methods.

提交回复
热议问题