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

后端 未结 14 2374
难免孤独
难免孤独 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:40

    Usually, if I find that sed can't do something I need, I turn to awk:

    awk '
    BEGIN {
        blank = 0;
    }
    
    /^[[:blank:]]*$/ {
         if (!blank) {
              print;
         }
         blank = 1;
         next;
    }
    
    {
         print;
         blank = 0;
    }' file
    

提交回复
热议问题