Splitting a big file according to the first colum value with bash
1
0
Entering edit mode
5.0 years ago
mel22 ▴ 100

Hi, Please I have a big file with recombiantion rate for all chromosomes, and I need to split it by chromosome (1 to 22) than delete the first column (chromosome name) than store it in tab separated txt file, I am vzry beginner with bash, do you have any idea ? these is a sample from my file :


    chr positions     combrates        Genetic_Map
    23 151344121 3.6300873858 191.277343851358438
    23 151344279 1.9852422405 191.277657519632442
    23 151344318 0.8949795125 191.277692423833429
    23 151345142 0.0757532087 191.277754844477414

thank you !

snp • 1.5k views
ADD COMMENT
0
Entering edit mode

Thank you all,
vkkodali method worked perfectly !

ADD REPLY
0
Entering edit mode

If an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one if they work.
Upvote|Bookmark|Accept

ADD REPLY
0
Entering edit mode

Thank you, I have already upvoted before you add your answer

ADD REPLY
2
Entering edit mode
5.0 years ago
vkkodali_ncbi ★ 3.8k

This is my preferred way of doing it.

awk 'BEGIN{FS="\t";OFS="\t"}{print > $1".txt"}' <input_file>

I am assuming that your data are tab-delimited. If that's not the case, change the FS and OFS accordingly. It will create files of the kind 23.txt based on your example.

ADD COMMENT
1
Entering edit mode

you meant

print >> $1".txt"
ADD REPLY
1
Entering edit mode

print > $1".txt" works fine. But if you are planning to do this for multiple input files, print >> $1".txt" is the way to go.

$ cat temp.bed 
chr1    100652477       100715409
chr11   175913961       176176380
chr1    150980972       151008189
chr11   6281252 6296044
chr1    20959947        20978004
chr11   32479294        32509482
chr1    27248212        27273362
chr11   31404352        31538564
chr1    36690016        36770957
chr11   46092975        46152302

$ awk 'BEGIN{FS="\t";OFS="\t"}{print > $1".txt"}' temp.bed
$ wc -l chr*.txt 
  5 chr11.txt
  5 chr1.txt
ADD REPLY

Login before adding your answer.

Traffic: 2067 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6