2013-04-29 118 views
8

我在每个摘要之间都有大文本文件,其中包含1000个带有空行的摘要。我想将这个文件分成1000个文本文件。 我的文件看起来像将文本文件分割成多个文件

16503654 Three-dimensional structure of neuropeptide k bound to dodecylphosphocholine micelles.  Neuropeptide K (NPK), an N-terminally extended form of neurokinin A (NKA), represents the most potent and longest lasting vasodepressor and cardiomodulatory tachykinin reported thus far. 

16504520 Computer-aided analysis of the interactions of glutamine synthetase with its inhibitors.  Mechanism of inhibition of glutamine synthetase (EC 6.3.1.2; GS) by phosphinothricin and its analogues was studied in some detail using molecular modeling methods. 
+0

我可能会建议,以避免在一个目录下创建太多的文件或目录。它肯定会减慢stat(2)调用。一千个不是一个大问题,但有一万个可以。当然,这个限制取决于您使用的机器(HDD),操作系统和文件系统。 – TrueY 2013-04-29 09:05:27

+0

可能重复[Split a .txt file based on content](http://stackoverflow.com/questions/8544684/split-a-txt-file-based-on-content) – tripleee 2013-06-28 04:35:09

+0

可能的重复[Split one file into多个文件基于分隔符](http://stackoverflow.com/questions/11313852/split-one-file-into-multiple-files-based-on-delimiter) – Gilles 2013-07-02 12:52:13

回答

26

您可以使用拆分并将“每个输出文件的NUMBER行数”设置为2.每个文件都有一个文本行和一个空行。

split -l 2 file 
4

事情是这样的:

awk 'NF{print > $1;close($1);}' file 

这将创建一个文件名作为抽象的数字1000个文件。这个awk代码将记录写入从第一个字段($ 1)检索其名称的文件。只有在字段数大于0的情况下才会执行此操作(NF)

+0

感谢您的快速response.It工作,但它显示awk: 9276016使得打开的文件太多 输入记录编号35,文件pmid.txt 源代码行号1.我对每个文件尝试了不同的文件,它在相同行号35处显示错误。是否有任何限制 – shalini 2013-04-29 07:43:02

+0

已更新命令以关闭文件适当。 – Guru 2013-04-29 07:47:19

+0

我面临另一个问题。我的文件有一些行以摘要中的结论或结果开头,在这种情况下,您提到的命令生成了一个带有“结论”和“结果”名称的额外文件,这是我不想要的。请帮我解决 – shalini 2013-05-10 13:12:29

4

您可以随时使用csplit命令。这是一个文件分割器,但基于正则表达式。

东西沿着线:

csplit -ks -f /tmp/files INPUTFILENAMEGOESHERE '/^$/' 

这是未经测试,可能需要一些调整,但。

CSPLIT

+0

我更喜欢'awk'解决方案。要用分隔块的空行分割一个大文件(LDIF格式),我使用'重复模式'和'压缩匹配行'选项:'csplit -m -f/tmp/files INPUTFILE'/^\ s * $/''{*}'' – bovender 2015-04-16 12:16:26

+0

对于csplit来说,万岁了。 +1。 – 2015-10-06 23:36:15

相关问题