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
原文地址:https://www.cnblogs.com/liujiaxin2018/p/15585708.html