且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用命令行将文本文件拆分为更小的多个文本文件

更新时间:2023-10-17 17:02:04

我知道这个问题很久以前就被问过了,但我很惊讶没有人给出最直接的unix答案:

I know the question has been asked a long time ago, but I am surprised that nobody has given the most straightforward unix answer:

split -l 5000 -d --additional-suffix=.txt $FileName file

  • -l 5000:将文件拆分为每个5000行的文件.
  • -d:数字后缀.这将使后缀默认从 00 变为 99,而不是从 aa 变为 zz.
  • --additional-suffix:让你指定后缀,这里是扩展名
  • $FileName:要分割的文件名.
  • file:添加到结果文件的前缀.
    • -l 5000: split file into files of 5,000 lines each.
    • -d: numerical suffix. This will make the suffix go from 00 to 99 by default instead of aa to zz.
    • --additional-suffix: lets you specify the suffix, here the extension
    • $FileName: name of the file to be split.
    • file: prefix to add to the resulting files.
    • 与往常一样,请查看 man split 了解更多详情.

      As always, check out man split for more details.

      对于 Mac,split 的默认版本显然被简化了.您可以使用以下命令安装 GNU 版本.(查看此问题了解更多 GNU 实用程序)

      For Mac, the default version of split is apparently dumbed down. You can install the GNU version using the following command. (see this question for more GNU utils)

brew install coreutils

然后您可以通过将 split 替换为 gsplit 来运行上述命令.查看 man gsplit 了解详情.

and then you can run the above command by replacing split with gsplit. Check out man gsplit for details.