如何将大型文本文件拆分为行数相等的较小文件?我有一个大的(按行数)纯文本文件,我想把它分割成更小的文件,也按行数来划分。因此,如果我的文件有大约200万行,我想将它分成10个包含200 k行的文件,或者100个包含20k行的文件(再加上一个包含其余部分的文件;是否均匀可除并不重要)。在Python中,我可以很容易地做到这一点,但我想知道是否有一种使用bash和unix utils(而不是手动循环和计数/分区行)的忍者方法。
3 回答
慕娘9325324
TA贡献1783条经验 获得超4个赞
$ 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
split -l 200000 filename
xaa xab xac
...
split -C 20m --numeric-suffixes input_filename output_prefix
output_prefix01 output_prefix02 output_prefix03 ...
牛魔王的故事
TA贡献1830条经验 获得超3个赞
split
$ 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 just before each output file is opened --help display this help and exit --version output version information and exit SIZE may have a multiplier suffix: b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024, GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.
- 3 回答
- 0 关注
- 414 浏览
添加回答
举报
0/150
提交
取消