Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split text file into multiple files

Tags:

bash

unix

awk

I am having large text file having 1000 abstracts with empty line in between each abstract . I want to split this file into 1000 text files. My file looks like

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. 
like image 774
shalini Avatar asked Apr 29 '13 07:04

shalini


People also ask

How do I split text into multiple files in Windows 10?

Click the Splitter tab and press the Input File button to select a file to split (or select the File Splitter Joiner option from file context menus). Note that file sizes must eclipse one megabyte. Next, press the Output Folder button to choose a folder to save the split files in. Then choose how to split the file.

How do I split a text file into multiple files using CMD?

To split a file into pieces, you simply use the split command. By default, the split command uses a very simple naming scheme. The file chunks will be named xaa, xab, xac, etc., and, presumably, if you break up a file that is sufficiently large, you might even get chunks named xza and xzz.


2 Answers

You can use split and set "NUMBER lines per output file" to 2. Each file would have one text line and one empty line.

split -l 2 file
like image 144
Alper Avatar answered Sep 29 '22 13:09

Alper


Something like this:

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

This will create 1000 files with filename being the abstract number. This awk code writes the records to a file whose name is retrieved from the 1st field($1). This is only done only if the number of fields is more than 0(NF)

like image 23
Guru Avatar answered Sep 29 '22 13:09

Guru