您的位置:首页 > 其它

inux删除文件中空行的几种方法

2015-08-14 09:27 253 查看
file文件内容如下
[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 #删除包含有空格的空行
[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
[root@test1 test]# grep -v "^\s*$" file
1
2 The world is not you can do, but you should.
o
三、awk
[root@test1 test]# awk NF file
1
2 The world is not you can do, but you should.
o
[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
剔除了含有空格的行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: