我有一个大的(按行数)纯文本文件,我想将其拆分为较小的文件,也按行数。因此,如果我的文件有大约 2M 行,我想将其拆分为 10 个包含 200k 行的文件,或 100 个包含 20k 行的文件(加上一个文件,其余部分;被均匀整除并不重要)。
我可以在 Python 中相当容易地做到这一点,但我想知道是否有任何忍者方法可以使用 Bash 和 Unix 实用程序来做到这一点(而不是手动循环和计算/分区行)。
cat part* > original
看一下拆分命令:
$ 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
这将创建文件,每个文件都有 200000 行,名为 xaa xab xac
...
另一种选择,按输出文件的大小拆分(仍然在换行符处拆分):
split -C 20m --numeric-suffixes input_filename output_prefix
创建像 output_prefix01 output_prefix02 output_prefix03 ...
这样的文件,每个最大大小为 20 兆字节。
是的,有一个 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.
split -l 1000000 train_file train_file.
并在同一目录中,我将获得第一个百万的 train_file.aa
,然后是下一个百万的 trail_file.ab
,等等。
split input my/dir/
。
将文件“file.txt”拆分为 10,000 行文件:
split -l 10000 file.txt
使用 split
:
将文件拆分为固定大小的块,创建包含连续 INPUT 部分的输出文件(如果没有给出标准输入或 INPUT 是 `-')
Syntax split [options] [INPUT [PREFIX]]
利用:
sed -n '1,100p' filename > output.txt
这里,1 和 100 是您将在 output.txt
中捕获的行号。
split
。
split
(来自 GNU coreutils,自 version 8.8 from 2010-12-22 起)包括以下参数:
-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
因此,split -n 4 input output.
将生成具有相同字节数的四个文件 (output.a{a,b,c,d}
),但中间可能会出现断行。
如果我们想保留完整的行(即按行分割),那么这应该有效:
split -n l/4 input output.
相关答案:https://stackoverflow.com/a/19031247
您还可以使用 AWK:
awk -vc=1 'NR%200000==0{++c}{print $0 > c".txt"}' largefile
awk -v lines=200000 -v fmt="%d.txt" '{print>sprintf(fmt,1+int((NR-1)/lines))}'
prefix
:awk -vc=1 'NR%200000==0{++c}{print $0 > "prefix"c".txt"}' largefile
如果您只想按每个文件的 x 行数拆分,则有关 split
的给定答案是可以的。但是,我很好奇为什么没有人关注这些要求:
“不必计算它们”-> 使用 wc + cut
“在额外文件中有剩余部分”-> 默认情况下会拆分
没有“wc + cut”我无法做到这一点,但我正在使用它:
split -l $(expr `wc $filename | cut -d ' ' -f3` / $chunks) $filename
这可以很容易地添加到您的 .bashrc 文件函数中,因此您可以调用它,传递文件名和块:
split -l $(expr `wc $1 | cut -d ' ' -f3` / $2) $1
如果您只想在额外文件中没有剩余的 x 块,只需调整公式以在每个文件上求和(块 - 1)。我确实使用这种方法,因为通常我只想要 x 个文件而不是每个文件 x 行:
split -l $(expr `wc $1 | cut -d ' ' -f3` / $2 + `expr $2 - 1`) $1
您可以将其添加到脚本中并将其称为您的“忍者方式”,因为如果没有适合您的需求,您可以构建它:-)
split
的 -n
选项。
要将大文本文件拆分为每个 1000 行的小文件:
split <file> -l 1000
要将大型二进制文件拆分为每个 10M 的较小文件:
split <file> -b 10M
要将拆分的文件合并为一个文件:
cat x* > <file>
拆分一个文件,每个拆分有 10 行(最后一个拆分除外):
split -l 10 filename
将一个文件拆分为 5 个文件。文件被拆分,使得每个拆分都具有相同的大小(最后一个拆分除外):
split -n 5 filename
在每个拆分中拆分一个 512 字节的文件(最后一个拆分除外;使用 512k 表示千字节,使用 512m 表示兆字节):
split -b 512 filename
在不换行的情况下,在每个拆分中拆分一个最多 512 个字节的文件:
split -C 512 filename
--> 作者:cht.sh
HDFS getmerge 小文件并拆分成合适的大小。
这种方法会导致换行:
split -b 125m compact.file -d -a 3 compact_prefix
我尝试 getmerge 并将每个文件拆分为大约 128 MB。
# Split into 128 MB, and judge sizeunit is M or G. Please test before use.
begainsize=`hdfs dfs -du -s -h /externaldata/$table_name/$date/ | awk '{ print $1}' `
sizeunit=`hdfs dfs -du -s -h /externaldata/$table_name/$date/ | awk '{ print $2}' `
if [ $sizeunit = "G" ];then
res=$(printf "%.f" `echo "scale=5;$begainsize*8 "|bc`)
else
res=$(printf "%.f" `echo "scale=5;$begainsize/128 "|bc`) # Celling ref http://blog.csdn.net/naiveloafer/article/details/8783518
fi
echo $res
# Split into $res files with a number suffix. Ref: http://blog.csdn.net/microzone/article/details/52839598
compact_file_name=$compact_file"_"
echo "compact_file_name: "$compact_file_name
split -n l/$res $basedir/$compact_file -d -a 3 $basedir/${compact_file_name}
不定期副业成功案例分享
split -b 200m filename
(m 表示兆字节,k 表示千字节或字节没有后缀)-d
选项在 OSX 上不可用,请改用gsplit
。希望这对 Mac 用户有用。