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

 

相关文章

Centos系统之Shell编程基础知识
从Export理解Shell环境和变量生存期
linux shell数组变量、类型及规则
Centos编程Shell基本工作原理方案
Centos操作系统编程之Shell 问答录
rsync-linux备份脚本