使用命令行将文本文件拆分为较小的多个文本文件 [英] Split text file into smaller multiple text file using command line
问题描述
我有大约100,000行的多个文本文件,我想将它们拆分为每个5000行的较小的文本文件.
I have multiple text file with about 100,000 lines and I want to split them into smaller text files of 5000 lines each.
我用过:
split -l 5000 filename.txt
创建文件:
xaa
xab
aac
xad
xbe
aaf
没有扩展名的文件.我只想给他们一个类似的名字:
files with no extensions. I just want to call them something like:
file01.txt
file02.txt
file03.txt
file04.txt
或者,如果这不可能,我只希望它们具有".txt"扩展名.
or if that is not possible, i just want them to have the ".txt" extension.
推荐答案
我知道很久以前就问过这个问题,但是我很惊讶没有人给出最直接的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
:将文件分成5,000行的文件. -
-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 utils的信息,请参见此问题. )
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.
这篇关于使用命令行将文本文件拆分为较小的多个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!