【问题标题】:How to set starting index in split command in linux ?如何在 linux 的 split 命令中设置起始索引?
【发布时间】:2014-07-02 20:24:07
【问题描述】:


我想根据 linux 中的行数将一个大文件拆分为多个文件。我读到了splitcsplit。这些命令提供了一个选项,通过使用 -d 选项为文件添加数值后缀,例如
split -d -l 100 largeFile.txt smallFile.txt. 将创建带有 smallFile.txt.00,smallFile.txt.01 .... 的小文件。但我希望起始索引为 01 而不是 00Man pages 没有提供太多关于此的信息。所以,请帮助我 有没有更好的方法来根据 linux 中的行数分割文件?

谢谢

【问题讨论】:

    标签: linux file unix split


    【解决方案1】:

    既然你说 Linux,那意味着 split 来自 GNU coreutils。如果您查看拆分代码的源代码,处理 -d / --numeric-suffixes 选项设置为采用可选参数,但对 getopt_long 的调用不会启用一个。也许是由于兼容性原因?你应该邮寄给他们问问。

    http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob_plain;f=src/split.c;hb=HEAD

    @@ -1153,7 +1153,7 @@ main (int argc, char **argv)
           int this_optind = optind ? optind : 1;
           char *slash;
    
    -      c = getopt_long (argc, argv, "0123456789C:a:b:del:n:u",
    +      c = getopt_long (argc, argv, "0123456789C:a:b:d:el:n:u",
                            longopts, NULL);
           if (c == -1)
             break;
    

    这个补丁可以处理可选的数字参数。然后你可以调用 split 为:

    split -d 1 foo foo.split.
    

    而且数字从 1 开始,而不是 0。

    您可以从http://www.gnu.org/software/coreutils/ 获取源代码并自己应用此更改,如果您愿意,可以在本地使用它。

    【讨论】:

    • 我们不允许短选项的可选参数,因为它引入了歧义。在这种情况下,如果您需要指定起点,只需使用 long 选项:
    • 我试了下还是不行,就去找源码。我的系统上可能有旧版本的拆分代码。
    【解决方案2】:

    自从 coreutils 发布 8.16 (2012-03-26) 你可以这样做:

    split --numeric=1 -l 100 largeFile.txt smallFile.txt.
    

    【讨论】:

      猜你喜欢
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多