文本处理 - 连续出现一行空行则删除这行空行,连续出现两行空行时则只删除一行空行留一行空行

Posted 王万林 Ben

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文本处理 - 连续出现一行空行则删除这行空行,连续出现两行空行时则只删除一行空行留一行空行相关的知识,希望对你有一定的参考价值。

处理需求

最近在编辑Markdown文本,从普通文本拷贝到code block时,总会多一行空行,链接在这。如何能删除多出来的空行?

规则

  • 连续出现一行空行则删除这行空行
  • 连续出现两行空行时则只删除一行空行留一行空行。

文本/tmp/test.txt

╭─ben at MacBook Pro in ~ 2021/08/16 - 21:18:29
╰─○ cat /tmp/test.txt                                 
group cpunode0 {

   

   cpuset {

​        cpuset.cpus = 0-5;

​       cpuset.mems = 0;

   }

 

   memory {

​        memory.limit_in_bytes = 32766M;

​       memory.memsw.limit_in_bytes = 32766M;

   }

 

}

 

 

group cpunode1 {

   cpuset {

​      cpuset.cpus = 6-11;

​       cpuset.mems = 1;

   }

 

   memory {

​        memory.limit_in_bytes = 32768M;

​       memory.memsw.limit_in_bytes = 32768M;

   }

 

}

 

group cpunode2 {

   cpuset {

​        cpuset.cpus = 18-23;

​       cpuset.mems = 2;

   }

 

   memory {

​        memory.limit_in_bytes = 32768M;

​       memory.memsw.limit_in_bytes = 32768M;

   }

 

}

 

 

group cpunode3 {

   cpuset {

​        cpuset.cpus = 12-17;

​       cpuset.mems = 3;

   }

 

   memory {

​        memory.limit_in_bytes = 32768M;

​       memory.memsw.limit_in_bytes = 32768M;

   }

 

}

实现

实现一:

╭─ben at MacBook Pro in ~ 2021/08/16 - 21:19:14
╰─○ cat /tmp/test.txt | sed -n '$!N;s/\\(.\\)\\n/\\1/;P;D'
group cpunode0 {
   
   cpuset {
​        cpuset.cpus = 0-5;
​       cpuset.mems = 0;
   }
 
   memory {
​        memory.limit_in_bytes = 32766M;
​       memory.memsw.limit_in_bytes = 32766M;
   }
 
}
 
 
group cpunode1 {
   cpuset {
​      cpuset.cpus = 6-11;
​       cpuset.mems = 1;
   }
 
   memory {
​        memory.limit_in_bytes = 32768M;
​       memory.memsw.limit_in_bytes = 32768M;
   }
 
}
 
group cpunode2 {
   cpuset {
​        cpuset.cpus = 18-23;
​       cpuset.mems = 2;
   }
 
   memory {
​        memory.limit_in_bytes = 32768M;
​       memory.memsw.limit_in_bytes = 32768M;
   }
 
}
 
 
group cpunode3 {
   cpuset {
​        cpuset.cpus = 12-17;
​       cpuset.mems = 3;
   }
 
   memory {
​        memory.limit_in_bytes = 32768M;
​       memory.memsw.limit_in_bytes = 32768M;
   }
 
}

实现二:

╭─ben at MacBook Pro in ~ 2021/08/16 - 21:19:43
╰─○ cat /tmp/test.txt | awk '{ print; if (length) getline }' 
group cpunode0 {
   
   cpuset {
​        cpuset.cpus = 0-5;
​       cpuset.mems = 0;
   }
 
   memory {
​        memory.limit_in_bytes = 32766M;
​       memory.memsw.limit_in_bytes = 32766M;
   }
 
}
 
 
group cpunode1 {
   cpuset {
​      cpuset.cpus = 6-11;
​       cpuset.mems = 1;
   }
 
   memory {
​        memory.limit_in_bytes = 32768M;
​       memory.memsw.limit_in_bytes = 32768M;
   }
 
}
 
group cpunode2 {
   cpuset {
​        cpuset.cpus = 18-23;
​       cpuset.mems = 2;
   }
 
   memory {
​        memory.limit_in_bytes = 32768M;
​       memory.memsw.limit_in_bytes = 32768M;
   }
 
}
 
 
group cpunode3 {
   cpuset {
​        cpuset.cpus = 12-17;
​       cpuset.mems = 3;
   }
 
   memory {
​        memory.limit_in_bytes = 32768M;
​       memory.memsw.limit_in_bytes = 32768M;
   }
 
}

参考资料

https://www.imooc.com/video/14507
https://unix.stackexchange.com/a/492415/287317
https://unix.stackexchange.com/a/492414/287317

以上是关于文本处理 - 连续出现一行空行则删除这行空行,连续出现两行空行时则只删除一行空行留一行空行的主要内容,如果未能解决你的问题,请参考以下文章

文本处理工具简略

linux删除空行操作:awkgreptrsed

文本处理工具

linux文本处理工具

16文本处理工具

基本文本处理工具