35

我正在使用以下命令来拆分文件。它应该每 50,000 行拆分一次,并使用 4 位数字后缀。该文件大约有 1.4 亿行。

split -d -l -n 4 50000 domains.xml domains_

但是当我运行时,我收到了这个错误:

split: -n: invalid number of lines
Try `split --help' for more information.

什么是正确的命令?

4

5 回答 5

76

由于 GNU 的主要帮助split说:

Usage: /usr/gnu/bin/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

在我看来,您需要重新组织一下您的选择:

split -a 4 -d -l 50000 domains.xml domains_
于 2013-02-20T07:38:49.043 回答
13

(来自手册页,GNU coreutils 8.21)您需要的似乎是 -a/--suffix-length=N (生成长度为 N 的后缀(默认为 2)),而不是 -n/--number=CHUNKS (生成 CHUNKS 输出文件)

split -d -l 50000 -a 4 domains.xml domains_

你应该得到:domains_0000,domains_0001 ...

于 2015-10-15T22:06:49.733 回答
7

我会用awk. 它使您可以更好地控制输出文件和文件名。也应该快点问。以下是将 100 行文件拆分为 20 行块的方法:

awk 'NR%20==1 { file = FILENAME "_" sprintf("%04d", NR+19) } { print > file }' domains.xml

这应该创建一些文件,例如:

file_0020
file_0040
file_0060
file_0080
file_0100

相应调整。HTH。

于 2013-02-20T07:02:51.010 回答
4

虽然您没有要求它,但我想您想要对结果文件进行适当的扩展(让我们说 xml):

split -d -l 50000 -a 4 --additional-suffix=.xml domains.xml domains_

--additional-suffix=.xml将使文件名类型domains_0000.xmldomains_1453.xml

于 2020-05-25T09:25:14.977 回答
-2

我不知道这是否对您有帮助,但是如果您在文件名前缀中添加 1,即outfile1您最终会得到:

outfile101
outfile102
outfile103

我知道这可能不是您要找的东西,但是无论计算机科学家是否“总是从零开始计数”,各种程序都不会解析作业数组等中的前导零。至少通过这种方式,您可以使用更广泛的程序解析您的文件。

于 2018-01-04T10:42:38.673 回答