linux系统中删除文件的最后几行教程
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