linux系统中同时提取多个文件的前几行并合并为新文件

 

1、head 直接实现

root@PC1:/home/test# ls
a.txt  b.txt  c.txt
root@PC1:/home/test# cat a.txt
1
2
3
4
5
root@PC1:/home/test# cat b.txt
06
07
08
09
10
root@PC1:/home/test# cat c.txt
11
12
13
14
15
root@PC1:/home/test# head -n 3 -q *.txt   ## 同时提取多有txt文件的前三行并合并, 同理可实现后几行提取并合并
1
2
3
06
07
08
11
12
13

 

2、for循环实现

root@PC1:/home/test# ls
a.txt  b.txt  c.txt
root@PC1:/home/test# cat a.txt
1
2
3
4
5
root@PC1:/home/test# cat b.txt
06
07
08
09
10
root@PC1:/home/test# cat c.txt
11
12
13
14
15
root@PC1:/home/test# for i in `ls *.txt`; do head -n 3 $i >> result.txt; done   ## for循环 + 追加重定向实现
root@PC1:/home/test# ls
a.txt  b.txt  c.txt  result.txt
root@PC1:/home/test# cat result.txt
1
2
3
06
07
08
11
12
13

 

相关文章

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