您的位置:首页 > 运维架构 > Linux

Linux下文件的切分与合并

2016-02-19 16:42 603 查看
一、切分

1、命令帮助


[spark@Master data-ssa]$ split --help
Usage: split [OPTION]... [INPUT [PREFIX]]
Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default
size is 1000 lines, and default PREFIX is `x'.  With no INPUT, or when INPUT
is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
-a, --suffix-length=N   use suffixes of length N (default 2)
-b, --bytes=SIZE        put SIZE bytes per output file
-C, --line-bytes=SIZE   put at most SIZE bytes of lines per output file
-d, --numeric-suffixes  use numeric suffixes instead of alphabetic
-l, --lines=NUMBER      put NUMBER lines per output file
--verbose           print a diagnostic just before each
output file is opened
--help     display this help and exit
--version  output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following:
KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

Report split bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'split invocation'


2、语法格式与选项说明

split命令的语法如下:

split [--help][--version][-a ][-b][-C ][-l ][要切割的文件][输出文件名前缀]

对应的参数描述如下: -a, --suffix-length=N

使用的后缀长度 (默认为 2)

-b, --bytes=SIZE

每个输出文件的字节大小

-C, --line-bytes=SIZE

每个输出文件每行的最大字节大小

-d, --numeric-suffixes

使用数字后缀代替字母后缀

-l, --lines=NUMBER

设定每个输出文件的行数

--help 显示帮助信息

--version

显示版本信息

----------------------------------------------------------------------------------

短选项 长选项 选项描述

-b –bytes=SIZE SIZE 值为每一输出档案的大小,单位为 byte。

-C –line-bytes=SIZE 每一输出档中,单行的最大 byte 数。

-d –numeric-suffixes 使用数字作为后缀。

-l –lines=NUMBER NUMBER 值为每一输出档的列数大小。</p> <p>短选项 长选项 选项描述

-b –bytes=SIZE SIZE 值为每一输出档案的大小,单位为 byte。

-C –line-bytes=SIZE 每一输出档中,单行的最大 byte 数。

-d –numeric-suffixes 使用数字作为后缀。

-l –lines=NUMBER NUMBER 值为每一输出档的列数大小。

3、示例说明

例1、以每个文件1000行分割


split命令分割文件成每个文件1000行,并且文件名依次为 [前缀]aa, [前缀]ab, [前缀]ac等,默认的前缀是X,每个文件的行数为1000行。

命令:

$ split mylog -l 1000

$ wc -l *

4450 mylog

1000 xaa

1000 xab

1000 xac

1000 xad

450 xae

例2、以每个文件20MB分割

分割文件为多个20MB的文件,附带-b选项。

命令:

$ split -b 20M logdata

$ ls -lh | tail -n +2

-rw------- 1 sathiya sathiya 102M Jul 25 18:47 logdata

-rw------- 1 sathiya sathiya 20M Jul 25 19:20 xaa

-rw------- 1 sathiya sathiya 20M Jul 25 19:20 xab

-rw------- 1 sathiya sathiya 20M Jul 25 19:20 xac

-rw------- 1 sathiya sathiya 20M Jul 25 19:20 xad

-rw------- 1 sathiya sathiya 20M Jul 25 19:20 xae

-rw------- 1 sathiya sathiya 1.6M Jul 25 19:20 xaf

例3、以每个文件50MB指定前缀分割

使用–bytes选项把文件分割成多个50MB的文件,–bytes类似-b选项,在第二个参数指定前缀。

命令:

$ split --bytes=50M logdata mydatafile

$ ls -lh

total 204M

-rw------- 1 sathiya sathiya 102M Jul 25 18:47 logdata

-rw------- 1 sathiya sathiya 50M Jul 25 19:23 mydatafileaa

-rw------- 1 sathiya sathiya 50M Jul 25 19:23 mydatafileab

-rw------- 1 sathiya sathiya 1.6M Jul 25 19:23 mydatafileac

例4、基于行数分割文件

使用-l选项指定行数来把文件分割成多个行数相同的文件。

命令:

$ wc -l testfile

2591 testfile

$ split -l 1500 testfile importantlog

$ wc -l *

1500 importantlogaa

1091 importantlogab

2591 testfile

例5、以数字后缀命名分割文件

使用-d选项可以指定后缀为数字,如00,01,02..,而不是aa,ab,ac。

命令:

$ split -d testfile

$ ls

testfile x00 x01 x02

二、合并

可以使用cat命令将切分后的文件合并成新的文件: $ cat split0* > original.txt
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: