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 @   小鲨鱼2018  阅读(171)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示