如何拆分一个大的文本文件转换成更小的文件具有同等数量的行? [英] How to split a large text file into smaller files with equal number of lines?

查看:286
本文介绍了如何拆分一个大的文本文件转换成更小的文件具有同等数量的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了我想要分割成更小的文件,也行数大(按行数)纯文本文件。所以,如果我的文件有大约2M线路,我想将它分割成包含20万行,或包含20K行100个文件10个文件(加上一个文件,其余部分;是整除无所谓)<。 / p>

我可以在Python做到这一点很容易,但我不知道是否有任何形式的忍者方式做到这一点使用bash和UNIX utils的(相对于手动循环和计数/分割线)。


解决方案

你有没有看着拆分命令?

  $分裂--help
用法:拆分[选项] [INPUT [preFIX]
输出固定大小的块输入到preFIXaa,preFIXab,...;默认
大小为1000线,并默认preFIX是'X'。无输入或输入时
是 - ,读取标准输入。强制性参数长选项是强制性的短选项时也是。
  -a,--suffix长度=长度N N使用的后缀(缺省为2)
  每个输出文件-b,--bytes = SIZE把SIZE字节
  -C,--line字节= SIZE把每路输出文件的行最SIZE字节
  -d,--numeric的后缀使用数字后缀代替字母
  每个输出文件-l,--lines = NUM​​BER把NUMBER线
      --verbose打印诊断标准错误只是
                            前的各输出文件打开
      --help显示帮助并退出
      版本输出版本信息并退出

您可以这样做:

 分裂-l 200000名

I've got a large (by number of lines) plain text file that I'd like to split into smaller files, also by number of lines. So if my file has around 2M lines, I'd like to split it up into 10 files that contain 200k lines, or 100 files that contain 20k lines (plus one file with the remainder; being evenly divisible doesn't matter).

I could do this fairly easily in Python but I'm wondering if there's any kind of ninja way to do this using bash and unix utils (as opposed to manually looping and counting / partitioning lines).

解决方案

Have you looked at the split command?

$ 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   use suffixes of length N (default 2)
  -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  use numeric suffixes instead of alphabetic
  -l, --lines=NUMBER      put NUMBER lines per output file
      --verbose           print a diagnostic to standard error just
                            before each output file is opened
      --help     display this help and exit
      --version  output version information and exit

You could do something like:

split -l 200000 filename

这篇关于如何拆分一个大的文本文件转换成更小的文件具有同等数量的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆