linux系统中删除文件的最后几行

时间:2021-07-09 16:36:56   收藏:0   阅读:19

1、测试数据

[root@centos79 test]# ls
a.txt
[root@centos79 test]# cat a.txt
3 4 5
d g 3
s g 8
k s g
2 5 d
s c w
a r t
e 4 s

 

2、head命令删除最后三行

[root@centos79 test]# cat a.txt
3 4 5
d g 3
s g 8
k s g
2 5 d
s c w
a r t
e 4 s
[root@centos79 test]# head -n -3 a.txt
3 4 5
d g 3
s g 8
k s g
2 5 d

 

3、sed命令删除最后3行

[root@centos79 test]# cat a.txt
3 4 5
d g 3
s g 8
k s g
2 5 d
s c w
a r t
e 4 s
[root@centos79 test]# tac a.txt | sed 1,3d | tac
3 4 5
d g 3
s g 8
k s g
2 5 d

 

4、sed删除最后3行

[root@centos79 test]# cat a.txt
3 4 5
d g 3
s g 8
k s g
2 5 d
s c w
a r t
e 4 s
[root@centos79 test]# a=$(sed -n "$=" a.txt )
[root@centos79 test]# echo $a
8
[root@centos79 test]# let b=a-3+1
[root@centos79 test]# sed $(($b)),$(($a))d a.txt
3 4 5
d g 3
s g 8
k s g
2 5 d

 

原文:https://www.cnblogs.com/liujiaxin2018/p/14990097.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!