Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split large file according to value in single column (AWK)

Tags:

I would like to split a large file (10^6 rows) according to the value in the 6th column (about 10*10^3 unique values). However, I can't get it working because of the number of records. It should be easy but it's taking hours already and I'm not getting any further.

I've tried two options:
Option 1

awk '{print > $6".txt"}' input.file
awk: cannot open "Parent=mRNA:Solyc06g051570.2.1.txt" for output (Too many open files)

Option 2

awk '{print > $6; close($6)}' input.file 

This doesn't cause an error but the files it creates contain only the last line corresponding to 'grouping' value $6

This is the beginning of my file, however, this file doesn't cause an error because it's so small:

exon    3688    4407    +   ID=exon:Solyc06g005000.2.1.1    Parent=mRNA:Solyc06g005000.2.1
exon    4853    5604    +   ID=exon:Solyc06g005000.2.1.2    Parent=mRNA:Solyc06g005000.2.1
exon    7663    7998    +   ID=exon:Solyc06g005000.2.1.3    Parent=mRNA:Solyc06g005000.2.1
exon    9148    9408    +   ID=exon:Solyc06g005010.1.1.1    Parent=mRNA:Solyc06g005010.1.1
exon    13310   13330   +   ID=exon:Solyc06g005020.1.1.1    Parent=mRNA:Solyc06g005020.1.1
exon    13449   13532   +   ID=exon:Solyc06g005020.1.1.2    Parent=mRNA:Solyc06g005020.1.1
exon    13711   13783   +   ID=exon:Solyc06g005020.1.1.3    Parent=mRNA:Solyc06g005020.1.1
exon    14172   14236   +   ID=exon:Solyc06g005020.1.1.4    Parent=mRNA:Solyc06g005020.1.1
exon    14717   14803   +   ID=exon:Solyc06g005020.1.1.5    Parent=mRNA:Solyc06g005020.1.1
exon    14915   15016   +   ID=exon:Solyc06g005020.1.1.6    Parent=mRNA:Solyc06g005020.1.1
exon    22106   22261   +   ID=exon:Solyc06g005030.1.1.1    Parent=mRNA:Solyc06g005030.1.1
exon    23462   23749   -   ID=exon:Solyc06g005040.1.1.1    Parent=mRNA:Solyc06g005040.1.1
exon    24702   24713   -   ID=exon:Solyc06g005050.2.1.3    Parent=mRNA:Solyc06g005050.2.1
exon    24898   25402   -   ID=exon:Solyc06g005050.2.1.2    Parent=mRNA:Solyc06g005050.2.1
exon    25728   25845   -   ID=exon:Solyc06g005050.2.1.1    Parent=mRNA:Solyc06g005050.2.1
exon    36352   36835   +   ID=exon:Solyc06g005060.2.1.1    Parent=mRNA:Solyc06g005060.2.1
exon    36916   38132   +   ID=exon:Solyc06g005060.2.1.2    Parent=mRNA:Solyc06g005060.2.1
exon    57089   57096   +   ID=exon:Solyc06g005070.1.1.1    Parent=mRNA:Solyc06g005070.1.1
exon    57329   58268   +   ID=exon:Solyc06g005070.1.1.2    Parent=mRNA:Solyc06g005070.1.1
exon    59970   60505   -   ID=exon:Solyc06g005080.2.1.24   Parent=mRNA:Solyc06g005080.2.1
exon    60667   60783   -   ID=exon:Solyc06g005080.2.1.23   Parent=mRNA:Solyc06g005080.2.1
exon    63719   63880   -   ID=exon:Solyc06g005080.2.1.22   Parent=mRNA:Solyc06g005080.2.1
exon    64143   64298   -   ID=exon:Solyc06g005080.2.1.21   Parent=mRNA:Solyc06g005080.2.1
exon    66964   67191   -   ID=exon:Solyc06g005080.2.1.20   Parent=mRNA:Solyc06g005080.2.1
exon    71371   71559   -   ID=exon:Solyc06g005080.2.1.19   Parent=mRNA:Solyc06g005080.2.1
exon    73612   73717   -   ID=exon:Solyc06g005080.2.1.18   Parent=mRNA:Solyc06g005080.2.1
exon    76764   76894   -   ID=exon:Solyc06g005080.2.1.17   Parent=mRNA:Solyc06g005080.2.1
exon    77189   77251   -   ID=exon:Solyc06g005080.2.1.16   Parent=mRNA:Solyc06g005080.2.1
exon    80044   80122   -   ID=exon:Solyc06g005080.2.1.15   Parent=mRNA:Solyc06g005080.2.1
exon    80496   80638   -   ID=exon:Solyc06g005080.2.1.14   Parent=mRNA:Solyc06g005080.2.1
like image 317
Elmer Avatar asked May 19 '13 13:05

Elmer


People also ask

How to split a file into multiple files using AWK?

AWK - 10 Examples to Split a File into Multiple Files 1. Split the file into 3 different files, one for each item. i.e, All records pertaining to Item1 into a file, records... 2. Split the files by having an extension of .txt to the new file names. $ awk -F, ' {print > $1".txt"}' file1 The only... ...

Why do we need to split files into smaller files?

The files can be split into multiple files either based on a condition, or based on a pattern or because the file is big and hence needs to split into smaller files. Let us consider a sample file with the following contents: 1.

How do I use multiple header lines in an AWK call?

If you have more than one input file, which all have a header line, you can simply place them all as arguments to the awk call, as in awk -F'|' ' ... ' a.txt b.txt c.txt ... If, however, only the first file has a header line, you would need to change FNR to NR in the first rule.

Why is my AWK file size so low?

As noted by Ed Morton, the simple approach only works if the number of different output files is small (max. around 10). GNU awk will still continue working, but become slower due to automatically closing and opening files in the background as needed; other awk implementations may simply fail due to "too many open files".


1 Answers

Option 2, use “>>” instead of “>”, to append.

awk '{print >> $6; close($6)}' input.file 
like image 165
Patrice Levesque Avatar answered Oct 13 '22 10:10

Patrice Levesque