Can I use the sed command to replace multiple empty line with one empty line?

前端 未结 2 1668
我在风中等你
我在风中等你 2021-02-02 09:16

I know there is a similar question in SO How can I replace mutliple empty lines with a single empty line in bash?. But my question is can this be implemented by just using the <

相关标签:
2条回答
  • 2021-02-02 09:44

    Give this a try:

    sed '/^$/N;/^\n$/D' inputfile
    
    0 讨论(0)
  • 2021-02-02 09:58

    You can do this by removing empty lines first and appending line space with G command:

    sed '/^$/d;G' text.txt
    

    Edit2: the above command will add empty lines between each paragraph, if this is not desired, you could do:

    sed -n '1{/^$/p};{/./,/^$/p}'
    

    Or, if you don't mind that all leading empty lines will be stripped, it may be written as:

    sed -n '/./,/^$/p'
    

    since the first expression just evaluates the first line, and prints it if it is blank.

    Here: -n option suppresses pattern space auto-printing, /./,/^$/ defines the range between at least one character and none character (i.e. empty space between newlines) and p tells to print this range.

    0 讨论(0)
提交回复
热议问题