linux 中split命令实现按行拆分文件(Split command in Linux to split files by line)

1、测试数据

root@PC1:/home/test# ls
test.map
root@PC1:/home/test# cat test.map
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384

2、split -l 或者 split -实现按行拆分文件

实现按每10行拆分test.map。

root@PC1:/home/test# ls
test.map
root@PC1:/home/test# cat test.map
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
root@PC1:/home/test# split -l 10 test.map   ## 按照每10行拆分文件
root@PC1:/home/test# ls    ## 默认生成的文件是以xaa、xab依次递增的形式命名
test.map  xaa  xab
root@PC1:/home/test# cat xaa  ##查看生成文件
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
root@PC1:/home/test# cat xab
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384

split – 等价于split -l:

root@PC1:/home/test# ls
test.map
root@PC1:/home/test# cat test.map
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
root@PC1:/home/test# split -10 test.map  ## 按照每10行拆分文件
root@PC1:/home/test# ls
test.map  xaa  xab
root@PC1:/home/test# cat xaa
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
root@PC1:/home/test# cat xab
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384

3、按照每五行拆分文件

root@PC1:/home/test# ls
test.map
root@PC1:/home/test# cat test.map
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
root@PC1:/home/test# split -l 5 test.map   ## 按照每5行拆分测试文件
root@PC1:/home/test# ls
test.map  xaa  xab  xac  xad
root@PC1:/home/test# cat xaa
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
root@PC1:/home/test# cat xab
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
root@PC1:/home/test# cat xac
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
root@PC1:/home/test# cat xad
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384

4、指定生成文件的前缀

root@PC1:/home/test# ls
test.map
root@PC1:/home/test# cat test.map
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
root@PC1:/home/test# split -l 10 test.map sub  ## 指定生成的文件的前缀是sub
root@PC1:/home/test# ls
subaa  subab  test.map
root@PC1:/home/test# cat subaa
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
root@PC1:/home/test# cat subab
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
root@PC1:/home/test# ls
test.map
root@PC1:/home/test# cat test.map
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
root@PC1:/home/test# split -l 8 test.map prefix  ## 指定前缀为prefix
root@PC1:/home/test# ls
prefixaa  prefixab  prefixac  test.map
root@PC1:/home/test# cat prefixaa
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
root@PC1:/home/test# cat prefixab
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
root@PC1:/home/test# cat prefixac
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384

5、-d参数指定后缀按照数字递增

root@PC1:/home/test# ls
test.map
root@PC1:/home/test# cat test.map
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
root@PC1:/home/test# split -l 5 -d test.map sub_  ## 按每5行拆分文件, 前缀为sub_, -d参数指定后缀按照数字递增
root@PC1:/home/test# ls
sub_00  sub_01  sub_02  sub_03  test.map
root@PC1:/home/test# cat sub_00
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
root@PC1:/home/test# cat sub_01
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
root@PC1:/home/test# cat sub_02
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
root@PC1:/home/test# cat sub_03
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
————————

1. Test data

root@PC1:/home/test# ls
test.map
root@PC1:/home/test# cat test.map
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384

2. Split – L or split – to split files by line

Split test by every 10 lines map。

root@PC1:/home/test# ls
test.map
root@PC1:/home/test# cat test.map
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
root@PC1:/home/test# split -l 10 test.map   ## 按照每10行拆分文件
root@PC1:/home/test# ls    ## 默认生成的文件是以xaa、xab依次递增的形式命名
test.map  xaa  xab
root@PC1:/home/test# cat xaa  ##查看生成文件
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
root@PC1:/home/test# cat xab
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384

Split – equivalent to split – L:

root@PC1:/home/test# ls
test.map
root@PC1:/home/test# cat test.map
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
root@PC1:/home/test# split -10 test.map  ## 按照每10行拆分文件
root@PC1:/home/test# ls
test.map  xaa  xab
root@PC1:/home/test# cat xaa
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
root@PC1:/home/test# cat xab
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384

3. Split the file every five lines

root@PC1:/home/test# ls
test.map
root@PC1:/home/test# cat test.map
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
root@PC1:/home/test# split -l 5 test.map   ## 按照每5行拆分测试文件
root@PC1:/home/test# ls
test.map  xaa  xab  xac  xad
root@PC1:/home/test# cat xaa
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
root@PC1:/home/test# cat xab
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
root@PC1:/home/test# cat xac
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
root@PC1:/home/test# cat xad
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384

4. Specifies the prefix of the generated file

root@PC1:/home/test# ls
test.map
root@PC1:/home/test# cat test.map
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
root@PC1:/home/test# split -l 10 test.map sub  ## 指定生成的文件的前缀是sub
root@PC1:/home/test# ls
subaa  subab  test.map
root@PC1:/home/test# cat subaa
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
root@PC1:/home/test# cat subab
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
root@PC1:/home/test# ls
test.map
root@PC1:/home/test# cat test.map
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
root@PC1:/home/test# split -l 8 test.map prefix  ## 指定前缀为prefix
root@PC1:/home/test# ls
prefixaa  prefixab  prefixac  test.map
root@PC1:/home/test# cat prefixaa
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
root@PC1:/home/test# cat prefixab
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
root@PC1:/home/test# cat prefixac
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384

5. The – D parameter specifies that the suffix is incremented by a number

root@PC1:/home/test# ls
test.map
root@PC1:/home/test# cat test.map
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384
root@PC1:/home/test# split -l 5 -d test.map sub_  ## 按每5行拆分文件, 前缀为sub_, -d参数指定后缀按照数字递增
root@PC1:/home/test# ls
sub_00  sub_01  sub_02  sub_03  test.map
root@PC1:/home/test# cat sub_00
01      SNP01   55910
02      SNP02   85204
03      SNP03   122948
04      SNP04   203750
05      SNP05   312707
root@PC1:/home/test# cat sub_01
06      SNP06   356863
07      SNP07   400518
08      SNP08   487423
09      SNP09   578716
10      SNP10   639876
root@PC1:/home/test# cat sub_02
11      SNP11   739050
12      SNP12   775181
13      SNP13   782445
14      SNP14   902647
15      SNP15   988021
root@PC1:/home/test# cat sub_03
16      SNP16   1146615
17      SNP17   1184243
18      SNP18   1235261
19      SNP19   1385609
20      SNP20   1401384