Split file by chromosome (row header) into new files

Viewed 128

I have a .txt file that looks like:

chr1
       -0.0040129697
       -0.0039704541
      ... (around 1,000 more numbers)
chr2
       0.0036610729
       0.0026386990
      ...
chr3
       -0.0001457086
       0.0006073254
      ...

and so on for the numbers 1-22 (so all 22 chromosomes are in this file). I want to split this file into 22 files based on the data for each chromosome, so the file for chr1 will look like:

 -0.0040129697
 -0.0039704541
 ... (more numbers)

with just the entries between chr1 and chr2 contained but not the actual row headers "chr1" and "chr2". I would prefer to do this in bash (ie using awk) but can do this in some other language too.

So far, I have tried using awk where if I do in the format

awk '/chr1/,/chr2/' gwe.txt > chr1.txt

It works but it contains "chr2" in the new file as well as includes the data for any of the other numbers that include a 1 or 2 (like 10,11,...,20,21,22). For the later nubers, it works but as I said keeps the last line of the "chr#", which I can easily solve by removing it. My issue is I am struggling to make this into a for loop, since I'm not sure how to do something in awk like this:

for num in {1..21}; do awk '/chr${num}/,/chr${num+1}/' gwe.txt > chr${num}.txt; done since I am getting an error on the ${num+1} portion.

3 Answers

I suggest with awk:

awk '$1 ~ /^chr/{close(name); name=$1; next} {print $1 >name}' file

using awk

Just store the file name for the "header" lines and print the others to that file. With only 22 files, there's no need to bother with explicitly closing them.

awk '/^c/ {file=$1; next} {print >file}'
Related