【问题标题】:How to split a file using a numeric suffix如何使用数字后缀拆分文件
【发布时间】:2013-02-05 02:21:04
【问题描述】:

我正在使用以下命令来拆分文件。它应该每 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.

什么是正确的命令?

【问题讨论】:

    标签: shell command-line ssh command


    【解决方案1】:

    由于 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_
    

    【讨论】:

    • 这应该是问题的正确答案。上一个是另一种答案,但这使用相同的命令解决了问题
    • 仅供参考,这在 macOS 上不起作用,因为 mac 的内置 split 命令不附带 -d 选项(并且没有具有不同名称的等效选项)
    • 在MacOS上,这个命令叫做gsplit(或者你需要安装它,因为MacOS自带的常规split没有这些选项)。但是,gsplit 不能在 -d 选项之后指定数字(总是从 0 开始计数)
    • @blitter — 我很好奇:gsplit 安装在您的 MacOS 机器上的哪个目录?我在 10.14.6 Mojave(不要问)或 10.15.5 Catalina 上都没有 gsplit
    • @JonathanLeffler 我发现了两次:/usr/local/bin/gsplit /usr/local/Cellar/coreutils/8.25/bin/gsplit 我可能在某个时候自己安装了它——MacOS 不太可能附带 GNU 软件。我只是想指出,MacOS 上存在这种替代方案,与gsplit 相比,普通的split 已经过时了。搜索整个硬盘的命令是:find / -name gsplit
    【解决方案2】:

    (来自联机帮助页,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...

    【讨论】:

      【解决方案3】:

      我会使用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。

      【讨论】:

        【解决方案4】:

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

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

        --additional-suffix=.xml 将生成domains_0000.xmldomains_1453.xml 等类型的文件名。

        【讨论】:

          【解决方案5】:

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

          outfile101
          outfile102
          outfile103
          

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-10-04
            • 2013-02-12
            • 2016-09-06
            • 1970-01-01
            • 1970-01-01
            • 2021-09-16
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多