linux系统中文件开头、结尾的空行

1、删除开头的空行

[root@centos79 test]# cat a.txt


a g r e
i x k like


a f g liker
a g r e
a f g liker


[root@centos79 test]# cp a.txt a.txt.bak
[root@centos79 test]# sed 's/^\s*//g' a.txt -i  ##清空空行中的空格和制表符
[root@centos79 test]# a=$(awk '{if($0 ~ /./){print NR}}' a.txt | head -n 1)  ## 首个非空行的行号
[root@centos79 test]# echo $a
3
[root@centos79 test]# let a=a-1
[root@centos79 test]# sed 1,$(($a))d a.txt  ## 删除开头的空行
a g r e
i x k like


a f g liker
a g r e
a f g liker

 

2、删除结尾的空行

[root@centos79 test]# a=$(awk '{if($0 ~ /./){print NR}}' a.txt | tail -n 1)
[root@centos79 test]# let a=a+1
[root@centos79 test]# b=$(sed -n "$=" a.txt)
[root@centos79 test]# echo $b
11
[root@centos79 test]# sed $(($a)),$(($b))d a.txt


a g r e
i x k like


a f g liker
a g r e
a f g liker

 

posted @ 2021-07-04 23:38  小鲨鱼2018  阅读(159)  评论(0编辑  收藏  举报