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

 

posted @ 2021-10-25 21:33  小鲨鱼2018  阅读(301)  评论(0编辑  收藏  举报