linux 大文件分隔和合并操作

目录

背景

大文件如何进行数据拆分合并?

split命令

合并-cat命令

split和cat命令格式

小结


背景

linux 不玩不知道,一玩吓一跳,如果会一些小命令,shell什么的,那你工作过程中会节省很多时间哦。

上次碰到了需要拆分文件的需求,本来打算写java或者python脚本分一下呢,还得用IO流,还得写代码,后来发现了一个好方法,,那就是直接通过linux里面的命令来进行操作,真尼玛爽,用着...   分享一下文件拆分和合并的操作...

大文件如何进行数据拆分合并?

split命令

可以指定按行数分割和按字节大小分割两种模式。

(1) 按行数分割

# -l 按行数分隔,300 表示行数, 所需拆分的文件 ,拆分后的文件前缀名
split -l 300 large_file.txt new_file_prefix

加上-d,使用数字后缀;加上--verbose,显示分割进度:(必须是linux,Mac不支持哦)

split -l 50000 -d large_file.txt part_ --verbose

(2) 按字节大小分割

 split -b 10m large_file.log new_file_prefix

合并-cat命令

cat part_* > merge_file.txt

split和cat命令格式

$ split --h
Usage: split [OPTION]... [FILE [PREFIX]]
Output pieces of FILE to PREFIXaa, PREFIXab, ...;
default size is 1000 lines, and default PREFIX is 'x'.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -a, --suffix-length=N   generate suffixes of length N (default 2)            后缀名称的长度 (默认为2) 
      --additional-suffix=SUFFIX  append an additional SUFFIX to file names
  -b, --bytes=SIZE        put SIZE bytes per output file                       每个输出文件的字节大小
  -C, --line-bytes=SIZE   put at most SIZE bytes of records per output file    每个输出文件每行的最大字节大小
  -d                      use numeric suffixes starting at 0, not alphabetic   使用数字后缀代替字母后缀
      --numeric-suffixes[=FROM]  same as -d, but allow setting the start value
  -e, --elide-empty-files  do not generate empty output files with '-n'        不产生空的输出文件
      --filter=COMMAND    write to shell COMMAND; file name is $FILE           写入到shell命令行
  -l, --lines=NUMBER      put NUMBER lines/records per output file             设定每个输出文件的行数,默认行数是1000行
  -n, --number=CHUNKS     generate CHUNKS output files; see explanation below  产生chunks文件
  -t, --separator=SEP     use SEP instead of newline as the record separator;  使用新字符分割
                            '\0' (zero) specifies the NUL character
  -u, --unbuffered        immediately copy input to output with '-n r/...'     无需缓存
      --verbose           print a diagnostic just before each                  显示分割进度
                            output file is opened
      --help     display this help and exit                                    显示帮助信息
      --version  output version information and exit                           显示版本信息

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).

CHUNKS may be:
  N       split into N files based on size of input
  K/N     output Kth of N to stdout
  l/N     split into N files without splitting lines/records
  l/K/N   output Kth of N to stdout without splitting lines/records
  r/N     like 'l' but use round robin distribution
  r/K/N   likewise but only output Kth of N to stdout

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/split>
or available locally via: info '(coreutils) split invocation'
$ cat --h
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.

With no FILE, or when FILE is -, read standard input.

  -A, --show-all           equivalent to -vET
  -b, --number-nonblank    number nonempty output lines, overrides -n
  -e                       equivalent to -vE
  -E, --show-ends          display $ at end of each line
  -n, --number             number all output lines
  -s, --squeeze-blank      suppress repeated empty output lines
  -t                       equivalent to -vT
  -T, --show-tabs          display TAB characters as ^I
  -u                       (ignored)
  -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
      --help     display this help and exit
      --version  output version information and exit

Examples:
  cat f - g  Output f's contents, then standard input, then g's contents.
  cat        Copy standard input to standard output.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/cat>
or available locally via: info '(coreutils) cat invocation'

小结

         使用文件拆分为了干什么呢?我的场景是为了提高数据传输速率,需要用到文件的拆分,感觉linux命令好强哦,一行代码搞定了别的语言需要好多行代码,好玩不? 在大数据领域内,文件拆分合并还是比较常见的哦~

         你学会了吗?

### 回答1: Linux文件合并指的是将多个文件合并成一个文件。可以使用cat命令将多个文件的内容合并到一个文件,也可以使用其他工具如sed、awk等进行文件合并。在使用cat命令时,可以使用重定向符号将多个文件的内容输出到一个文件,例如: cat file1.txt file2.txt > merged.txt 这将把file1.txt和file2.txt的内容合并到merged.txt文件。 ### 回答2: 在Linux系统,我们可以使用不同的方法将文件合并在一起。下面我将介绍三种常见的方法: 1. 使用cat命令:cat命令用于连接文件并打印到标准输出上。我们可以使用cat命令将多个文件的内容合并成一个文件。例如,要将file1.txt和file2.txt合并成一个新的文件file3.txt,可以使用以下命令: ``` cat file1.txt file2.txt > file3.txt ``` 这将把file1.txt和file2.txt的内容连接起来,并将结果输出到file3.txt文件。 2. 使用合并命令(merge):有些Linux发行版提供了merge命令,用于合并两个或多个文件并将结果输出到一个新文件。例如,要合并file1.txt和file2.txt到新文件file3.txt,可以使用以下命令: ``` merge file1.txt file2.txt > file3.txt ``` merge命令会按照文件每行的字典顺序进行合并,并将结果输出到file3.txt文件。 3. 使用redirect(重定向)操作符和append(追加)操作符:除了使用cat命令和merge命令外,我们还可以使用重定向和追加操作符将一个文件的内容附加到另一个文件。例如,要将file1.txt的内容附加到file2.txt文件的末尾,可以使用以下命令: ``` cat file1.txt >> file2.txt ``` 这将把file1.txt的内容追加到file2.txt的末尾。 总之,以上是在Linux系统合并文件的三种常见方法。可以根据实际情况选择其一种方法来合并文件。 ### 回答3: 对于Linux文件合并,可以使用多种命令和方法来实现。 1. cat命令:cat命令是用于连接文件并打印在标准输出上的命令。可以将多个文件连接在一起,然后输出合并后的结果文件。例如,要将file1和file2合并到file3,可以使用以下命令: cat file1 file2 > file3 2. paste命令:paste命令用于将多个文件按列合并在一起。该命令默认使用制表符来作为分隔符,可以使用-d选项指定其他分隔符。例如,要将file1和file2按列合并到file3,可以使用以下命令: paste file1 file2 > file3 3. sort命令:sort命令用于对文件进行排序,并且可以将排序后的结果写入到其他文件。可以利用这个特性将多个文件的内容进行合并。例如,要将file1和file2的内容按照字母顺序合并到file3,可以使用以下命令: sort -m file1 file2 -o file3 无论使用哪种方法,都可以根据实际情况选择最合适的命令来进行文件合并操作。这些命令提供了不同的选项和参数,可以根据需要进行灵活的操作
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MrZhangBaby

请博主喝杯奶茶

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值