【问题标题】:Split text file into multiple files将文本文件拆分为多个文件
【发布时间】:2013-04-22 18:57:40
【问题描述】:

我有一个包含 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. 

【问题讨论】:

标签: bash unix awk


【解决方案1】:

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

split -l 2 file

【讨论】:

    【解决方案2】:

    类似这样的:

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

    这将创建 1000 个文件,其中文件名是摘要编号。此 awk 代码将记录写入文件,该文件的名称是从第一个字段 ($1) 中检索的。仅当字段数大于 0(NF) 时才这样做

    【讨论】:

    • 感谢您的快速响应。它工作但它显示 awk:9276016 使打开的文件过多,输入记录号 35,文件 pmid.txt 源行号 1。我为每个文件尝试了不同的文件,它显示错误在同一行号 35。它有任何限制
    • 我遇到了另一个问题。我的文件在摘要编号下有一些以结论或结果开头的行,在这种情况下,您提到的命令会生成一个带有我不想要的结论和结果名称的额外文件。请帮帮我
    【解决方案3】:

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

    类似于:

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

    它未经测试,但可能需要稍作调整。

    CSPLIT

    【讨论】:

    • 我更喜欢这个而不是“awk”解决方案。为了用空行分隔一个大文件(LDIF 格式),我使用了“重复模式”和“抑制匹配行”选项:csplit -m -f /tmp/files INPUTFILE '/^\s*$/' '{*}'
    • 是的,csplit 万岁。 +1。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多