How to split a large text file into smaller files with equal number of lines?

前端 未结 10 673
一整个雨季
一整个雨季 2020-11-22 16:42

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

10条回答
  •  孤独总比滥情好
    2020-11-22 17:38

    split (from GNU coreutils, since version 8.8 from 2010-12-22) includes the following parameter:

    -n, --number=CHUNKS     generate CHUNKS output files; see explanation below
    
    CHUNKS may be:
      N       split into N files based on size of input
      K/N     output Kth of N to stdout
      l/N     split into N files without splitting lines/records
      l/K/N   output Kth of N to stdout without splitting lines/records
      r/N     like 'l' but use round robin distribution
      r/K/N   likewise but only output Kth of N to stdout
    

    Thus, split -n 4 input output. will generate four files (output.a{a,b,c,d}) with the same amount of bytes, but lines might be broken in the middle.

    If we want to preserve full lines (i.e. split by lines), then this should work:

    split -n l/4 input output.
    

    Related answer: https://stackoverflow.com/a/19031247

提交回复
热议问题