我有一个大的基因组数据文件(.txt),格式如下。我想根据染色体列chr1, chr2..chrX,chrY
对其进行拆分,然后在所有拆分的文件中保留标题行。如何使用unix/linux命令执行此操作?
基因组数据
variantId chromosome begin end
1 1 33223 34343
2 2 44543 46444
3 2 55566 59999
4 3 33445 55666
结果
file.chr1.txt
variantId chromosome begin end
1 1 33223 34343
file.chr2.txt
variantId chromosome begin end
2 2 44543 46444
3 2 55566 59999
file.chr3.txt
variantId chromosome begin end
4 3 33445 55666
最佳答案
这是人类基因组的数据吗(即总是46条染色体)?如果是,这是怎么回事:
for chr in $(seq 1 46)
do
head -n1 data.txt >chr$chr.txt
done
awk 'NR != 1 { print $0 >>("chr"$2".txt") }' data.txt
(这是第二次编辑,基于上面@Sasha的评论。)
注意,
("chr"$2".txt")
周围的paren显然不需要在GNU awk上,但它们是在我的OS X版本awk上。关于linux - 根据列值拆分文本文件(基因组数据),并保留标题行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34388173/