繁体   English   中英

Linux:将文件列拆分为多个文件

[英]Linux: Split columns of file into multiple files

最好的方法(最简单的方法)是:将一个具有许多列(〜600000)的大文件分割成6个文件,并在6个文件中均匀分布各列? 另外,我希望每个文件都具有原始文件的前6列

例:

 FID     IID     MID     PID     SEX     PHENO   SNP1    SNP2    SNP3    SNP4
 1       70323   0       0       2       2       0       0       1       0    ...
 2       70323   0       0       2       2       1       0       2       1    ...
 3       70323   0       0       2       2       0       0       0       1    ...
 ...

使用可用于ubuntu的基本linux命令行功能的解决方案会更好(或perl / python脚本)

我在PERL中的解决方案:这是我在Perl中所做的。 它非常难看,所以我希望会有一个简单而优雅的解决方案。

#!/usr/bin/perl
use warnings;
use strict;

my $line;
my $num_snps=0;
my $i=0;

my $OUT1;
my $OUT2;
my $OUT3;
my $OUT4;

my $end1;
my $end2;
my $end3;
my $end4;
while($line = <>){
        chomp $line;
        my @a = split(/\s/,$line);
        if($i==0){
                $num_snps = $#a + 1 - 6;

                $end1 = 5+int($num_snps/4);
                $end2 = $end1+int($num_snps/4)+1;
                $end3 = $end2+int($num_snps/4)+1;
                $end4 = $#a;

                print("Breaks: $end1\t$end2\t$end3\t$end4\tTotal SNPs: $num_snps\n");
        }else{
                open($OUT1 , ">>kuehn1.raw");
                print $OUT1 join(" ",@a[0..5])." ".join(" ", @a[6..$end1])."\n";
                close($OUT1);
                open($OUT2 , ">>kuehn2.raw");
                print $OUT2 join(" ",@a[0..5])." ".join(" ", @a[($end1+1)..$end2])."\n";
                close($OUT2);
                open($OUT3 , ">>kuehn3.raw");
                print$OUT3 join(" ",@a[0..5])." ".join(" ", @a[($end2+1)..$end3])."\n";
                close($OUT3);
                open($OUT4 , ">>kuehn4.raw");
                print$OUT4 join(" ",@a[0..5])." ".join(" ", @a[($end3+1)..$end4])."\n";
                close($OUT4);
        }
        $i=$i+1;
}

您可以首先使用以下命令获取文件中的列数

awk '{ print NF}' < file

然后使用这些知识来构建您的断点。 因此,如果您的文件有66列

cut -f1-6,7-16 < file > file1
cut -f1-6,17-26 < file > file2
cut -f1-6,27-36 < file > file3
cut -f1-6,37-46 < file > file4
cut -f1-6,47-56 < file > file5
cut -f1-6,57-66 < file > file6

不是最优雅,但应该可以

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM