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