How can I replace multiple empty lines with a single empty line in bash?

后端 未结 14 2285
难免孤独
难免孤独 2020-12-13 06:14

I have a file that contains:

something



something else

something else again

I need a bash command, sed/grep w.e that will produce the fo

相关标签:
14条回答
  • 2020-12-13 06:49

    For BSD-derived systems (including GNU):

    You just need cat with the -s option which causes it to remove repeated empty lines from its output:

    cat -s
    

    From man page: -s --squeeze-blank: suppress repeated empty output lines.

    0 讨论(0)
  • 2020-12-13 06:52

    Super easy to do with vim. Just open the file and type the following:

    :%s/\n\n\n*/\r\r/
    

    That will reduce all blocks of more than 2 new lines to 2 new lines. Hope this helps!

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