linux中如何删除文件的最后几行

1、测试数据

root@PC1:/home/test2# ls
a.txt
root@PC1:/home/test2# cat a.txt
01
02
03
04
05
06
07
08
09
10

 

2、head 实现

root@PC1:/home/test2# ls
a.txt
root@PC1:/home/test2# cat a.txt
01
02
03
04
05
06
07
08
09
10
root@PC1:/home/test2# head -n -3 a.txt   ## 删除最后3行
01
02
03
04
05
06
07

 

3、sed实现

root@PC1:/home/test2# ls
a.txt
root@PC1:/home/test2# cat a.txt
01
02
03
04
05
06
07
08
09
10
root@PC1:/home/test2# tac a.txt
10
09
08
07
06
05
04
03
02
01
root@PC1:/home/test2# tac a.txt | sed '1,3d' | tac   ## 删除后三行
01
02
03
04
05
06
07

 

posted @ 2022-03-23 17:59  小鲨鱼2018  阅读(817)  评论(0编辑  收藏  举报