linux shell脚本实现删除连续的空行为一行

 

001、awk实现

复制代码
[root@PC1 test02]# ls
a.txt
[root@PC1 test02]# cat a.txt          ## 测试数据
01 02 03 04 05
06 07 08 09 10



11 12 13 14 15
16 17 18 19 20

21 22 23 24 25
26 27 28 29 30                        ## 将多个连续的空行压缩为一个空行
[root@PC1 test02]# awk 'BEGIN{tag = 0} {if($0 ~ /^$/) {tag++} else {tag = 0}; if(tag > 1) {next} else {print $0}}' a.txt
01 02 03 04 05
06 07 08 09 10

11 12 13 14 15
16 17 18 19 20

21 22 23 24 25
26 27 28 29 30
复制代码

 

002、sed实现

复制代码
[root@PC1 test02]# ls
a.txt
[root@PC1 test02]# cat a.txt
01 02 03 04 05
06 07 08 09 10



11 12 13 14 15
16 17 18 19 20

21 22 23 24 25
26 27 28 29 30
[root@PC1 test02]# sed '/^$/{N;/^\n$/d}' a.txt    ## 将多个连续的空行转换为单个空行
01 02 03 04 05
06 07 08 09 10

11 12 13 14 15
16 17 18 19 20

21 22 23 24 25
26 27 28 29 30
复制代码

 。

 

posted @   小鲨鱼2018  阅读(89)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2022-07-11 plink 软件 输出日志文件中 founders 和 nonfounders指的是什么?
2022-07-11 Warning: 225 het. haploid genotypes present (see test.hh ) 产生原因
2022-07-11 linux 中如何在文件指定的位置插入特定的行
2022-07-11 linux 中 如何在文件指定位置插入特定的列
点击右上角即可分享
微信分享提示