linux系统中如何删除空行。
1、测试数据 a.txt
[root@centos79 test]# ls a.txt [root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# cat -A a.txt a g r e$ $ ## 空行什么也没有 i x k like$ a f g liker$ [root@centos79 test]# sed -n l a.txt a g r e$ $ ## 空行什么也没有 i x k like$ a f g liker$
2、删除空行
[root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# grep -v "^$" a.txt a g r e i x k like a f g liker [root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# sed '/^$/d' a.txt a g r e i x k like a f g liker [root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# awk '/./{print}' a.txt a g r e i x k like a f g liker
继续
[root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# awk NF a.txt a g r e i x k like a f g liker [root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# sed '/^\s*$/d' a.txt a g r e i x k like a f g liker [root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# sed '/^[\t ]*$/d' a.txt a g r e i x k like a f g liker
3、修改测试文件,在空行中添加空格或者水平制表符
修改后的测试文件:
[root@centos79 test]# ls a.txt [root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# cat -A a.txt $ a g r e$ $ i x k like$ ^I^I$ a f g liker$ [root@centos79 test]# sed -n l a.txt $ a g r e$ $ i x k like$ \t\t$ a f g liker$
4、进行测试
[root@centos79 test]# ls a.txt [root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# grep -v "^$" a.txt a g r e i x k like a f g liker [root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# sed '/^$/d' a.txt a g r e i x k like a f g liker [root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# awk '/./{print}' a.txt a g r e i x k like a f g liker
继续
[root@centos79 test]# ls a.txt [root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# awk NF a.txt a g r e i x k like a f g liker [root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# sed '/^\s*$/d' a.txt a g r e i x k like a f g liker [root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# sed '/^[\t ]*$/d' a.txt a g r e i x k like a f g liker
5、补充
[root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# cat -A a.txt a g r e$ $ i x k like$ a f g liker$ [root@centos79 test]# awk '$0 != ""' a.txt a g r e i x k like a f g liker
6、补充
[root@centos79 test]# ls a.txt [root@centos79 test]# cat a.txt a g r e i x k like a f g liker [root@centos79 test]# cat -A a.txt a g r e$ $ i x k like$ a f g liker$ [root@centos79 test]# cat a.txt |tr -s "\n" a g r e i x k like a f g liker