-1

I have a big huge file e.g

chr1    1    G    300
chr1    2    A    500
chr1    3    C    200
chr4    1    T    35
chr4    2    G    400
chr4    3    C    435
chr3    1    G    300
chr3    2    A    500
chr3    3    C    200
chr3    1    T    35
chr3    2    G    400
chr6    3    C    435
chr6    4    A    223
chr6    5    T    400
chr6    6    G    300

I to split the file in multiple files by grouping the first column values e.g

File1.txt

chr1 1 G 300
chr1 2 A 500
chr1 3 C 200

file 3.txt

chr3 1 G 300
chr3 2 A 500
chr3 3 C 200
chr3 1 T 35
chr3 2 G 400
2

Awk solution:

awk '{ print > ("file" substr($1, 4) ".txt") }' file
  • substr($1, 4) - extract substring from the 1st field value $1 starting from position 4

Viewing results:

$ head file[0-9]*.txt
==> file1.txt <==
chr1    1    G    300
chr1    2    A    500
chr1    3    C    200

==> file3.txt <==
chr3    1    G    300
chr3    2    A    500
chr3    3    C    200
chr3    1    T    35
chr3    2    G    400

==> file4.txt <==
chr4    1    T    35
chr4    2    G    400
chr4    3    C    435

==> file6.txt <==
chr6    3    C    435
chr6    4    A    223
chr6    5    T    400
chr6    6    G    300
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.