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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM