如何使用bash将大文件拆分成许多小文件?

Yis*_*ang 21 bash

我有一个文件,比如all2000行,我希望它可以分成4个小文件,行号为1~500,501~1000,1001~1500,1501~2000.

也许,我可以这样做:

cat all | head -500 >small1
cat all | tail -1500 | head -500 >small2
cat all | tail -1000 | head -500 >small3
cat all | tail -500 >small4
Run Code Online (Sandbox Code Playgroud)

但是这种方式涉及行号的计算,当行数不是一个好的数字时,或者当我们想要将文件拆分成太多的小文件时(例如:all带有3241行的文件,我们想要将它分成7个文件,每个文件有463行).

有一个更好的方法吗?

Wil*_*ell 38

如果要拆分文件,请使用split:

split -l 500 all all
Run Code Online (Sandbox Code Playgroud)

将文件拆分为多个文件,每个文件有500行.如果要将文件拆分为大小相同的4个文件,请使用以下内容:

split -l $(( $( wc -l < all ) / 4 + 1 )) all all
Run Code Online (Sandbox Code Playgroud)

  • 有趣的是,即使原始集合中有足够的行,在我看来,使用"split -l"获取始终相同数量的文件也是不可能的.例如,考虑一组5行分成4个文件.要求每个文件放置1行将产生5个文件,而不是4.要求每个文件放置2行将产生3个文件,而不是4."split -n"可能是正确的方法,如果您的版本"拆分" "有它. (2认同)

Joh*_*die 10

查看split命令,它应该做你想要的(以及更多):

$ 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   generate suffixes of length N (default 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 lines per output file
  -d, --numeric-suffixes[=FROM]  use numeric suffixes instead of alphabetic.
                                   FROM changes the start value (default 0).
  -e, --elide-empty-files  do not generate empty output files with '-n'
      --filter=COMMAND    write to shell COMMAND; file name is $FILE
  -l, --lines=NUMBER      put NUMBER lines per output file
  -n, --number=CHUNKS     generate CHUNKS output files.  See below
  -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

SIZE is an integer and optional unit (example: 10M is 10*1024*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
l/K/N   output Kth of N to stdout without splitting lines
r/N     like 'l' but use round robin distribution
r/K/N   likewise but only output Kth of N to stdout
Run Code Online (Sandbox Code Playgroud)