微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

linux系统中如何删除空行

1、测试数据

root@PC1:/home/test/test/test# cat a.txt
1 d f

2 3 f
3 s 8


4 f a
d g 8
root@PC1:/home/test/test/test# cat -A a.txt
1 d f$
$
2 3 f$
3 s 8$
$
$
4 f a$
d g 8$

 

删除完全的空行, 不包含空格和制表符)

2、sed

root@PC1:/home/test/test/test# cat a.txt
1 d f

2 3 f
3 s 8


4 f a
d g 8
root@PC1:/home/test/test/test# sed '/^$/d' a.txt
1 d f
2 3 f
3 s 8
4 f a
d g 8
root@PC1:/home/test/test/test# cat a.txt
1 d f

2 3 f
3 s 8


4 f a
d g 8
root@PC1:/home/test/test/test# sed -n '/./p' a.txt
1 d f
2 3 f
3 s 8
4 f a
d g 8

 

2、grep

root@PC1:/home/test/test/test# cat a.txt
1 d f

2 3 f
3 s 8


4 f a
d g 8
root@PC1:/home/test/test/test# grep -v "^$" a.txt
1 d f
2 3 f
3 s 8
4 f a
d g 8
root@PC1:/home/test/test/test# cat a.txt
1 d f

2 3 f
3 s 8


4 f a
d g 8
root@PC1:/home/test/test/test# grep "^." a.txt
1 d f
2 3 f
3 s 8
4 f a
d g 8

 

3、awk

root@PC1:/home/test/test/test# cat a.txt
1 d f

2 3 f
3 s 8


4 f a
d g 8
root@PC1:/home/test/test/test# awk '$1 ~ /./ {print $0}' a.txt
1 d f
2 3 f
3 s 8
4 f a
d g 8
root@PC1:/home/test/test/test# cat a.txt
1 d f

2 3 f
3 s 8


4 f a
d g 8
root@PC1:/home/test/test/test# awk '/./ {print $0}' a.txt
1 d f
2 3 f
3 s 8
4 f a
d g 8

 

root@PC1:/home/test/test/test# cat a.txt
1 d f

2 3 f
3 s 8


4 f a
d g 8
root@PC1:/home/test/test/test# awk '$0 != ""' a.txt
1 d f
2 3 f
3 s 8
4 f a
d g 8

 

包含空格或支付表的情况。

1、测试数据

root@PC1:/home/test/test/test# cat a.txt
1 d f

2 3 f
3 s 8


4 f a
d g 8
root@PC1:/home/test/test/test# sed -n l a.txt
1 d f$
$
2 3 f$
3 s 8$
   $
\t\t$
4 f a$
d g 8$

 

2、sed

root@PC1:/home/test/test/test# cat a.txt
1 d f

2 3 f
3 s 8


4 f a
d g 8
root@PC1:/home/test/test/test# sed '/^[\t ]*$/d' a.txt
1 d f
2 3 f
3 s 8
4 f a
d g 8

 

root@PC1:/home/test/test/test# cat a.txt
1 d f

2 3 f
3 s 8


4 f a
d g 8
root@PC1:/home/test/test/test# sed '/^\s*$/d' a.txt
1 d f
2 3 f
3 s 8
4 f a
d g 8

 

2、awk

root@PC1:/home/test/test/test# cat a.txt
1 d f

2 3 f
3 s 8


4 f a
d g 8
root@PC1:/home/test/test/test# awk NF a.txt
1 d f
2 3 f
3 s 8
4 f a
d g 8

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。