linux 中删除文件的最后几行

 

001、

[root@PC1 test]# ls
a.txt
[root@PC1 test]# cat a.txt
1
2
3
4
5
6
7
8
9
10
[root@PC1 test]# head -n -3 a.txt    ## 删除最后3行
1
2
3
4
5
6
7

 

 

002、

[root@PC1 test]# ls
a.txt
[root@PC1 test]# cat a.txt
1
2
3
4
5
6
7
8
9
10
[root@PC1 test]# tac a.txt | sed '1,3d' | tac           ## 删除文件最后3行
1
2
3
4
5
6
7

 

 

003、

[root@PC1 test]# ls
a.txt
[root@PC1 test]# cat a.txt
1
2
3
4
5
6
7
8
9
10
[root@PC1 test]# awk -v a=$(wc -l <a.txt) 'NR <= a-3' a.txt    ## 删除文件的最后3行
1
2
3
4
5
6
7

 

posted @ 2022-12-08 23:41  小鲨鱼2018  阅读(1131)  评论(0编辑  收藏  举报