file文件内容如下
1
2
3
4
5
|
[root@test1 test]# cat file
1
2 The world is not you can do, but you should.
o
|
一、 sed
sed '/^$/d' file #删除空行
sed '/^\s*$/d' file #删除包含有空格的空行
1
2
3
4
5
6
7
8
9
10
|
[root@test1
test
]
# sed '/^$/d' file
1
2 The world is not you can
do
, but you should.
o
[root@test1
test
]
#
[root@test1
test
]
# sed '/^\s*$/d' file
1
2 The world is not you can
do
, but you should.
o
|
二、grep
1
2
3
4
|
[root@test1
test
]
# grep -v "^\s*$" file
1
2 The world is not you can
do
, but you should.
o
|
三、awk
1
2
3
4
|
[root@test1 test]# awk NF file
1
2 The world is not you can do, but you should.
o
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@test1 test]# awk '!/^$/' file
1
2 The world is not you can do, but you should.
o
未剔除含有空格的空行
[root@test1 test]# awk '!/^[[:space:]]+/ && !/^$/' file
1
2 The world is not you can do, but you should.
o
剔除了含有空格的行
|
本文转自 xoyabc 51CTO博客,原文链接:http://blog.51cto.com/xoyabc/1684530,如需转载请自行联系原作者