使用Perl将数据块转换为表格格式

时间:2010-04-01 06:37:29

标签: linux perl unix

我有一个看起来像这样的数据

1:SRX000566
Submitter: WoldLab
Study: RNASeq expression profiling for ENCODE project(SRP000228)
Sample: Human cell line GM12878(SRS000567)
Instrument: Solexa 1G Genome Analyzer
Total: 4 runs, 62.7M spots, 2.1G bases
Run #1: SRR002055, 11373440 spots, 375323520 bases
Run #2: SRR002063, 22995209 spots, 758841897 bases
Run #3: SRR005091, 13934766 spots, 459847278 bases
Run #4: SRR005096, 14370900 spots, 474239700 bases

2:SRX000565
Submitter: WoldLab
Study: RNASeq expression profiling for ENCODE project(SRP000228)
Sample: Human cell line GM12878(SRS000567)
Instrument: Solexa 1G Genome Analyzer
Total: 3 runs, 51.2M spots, 1.7G bases
Run #1: SRR002052, 12607931 spots, 416061723 bases
Run #2: SRR002054, 12880281 spots, 425049273 bases
Run #3: SRR002060, 25740337 spots, 849431121 bases

3:SRX012407
Submitter: GEO
Study: GSE17153: Illumina sequencing of small RNAs from C. elegans embryos(SRP001363)
Sample: Caenorhabditis elegans(SRS006961)
Instrument: Illumina Genome Analyzer II
Total: 1 run, 3M spots, 106.8M bases
Run #1: SRR029428, 2965597 spots, 106761492 bases

是否有一种紧凑的方式将它们转换为表格格式(制表符分隔)。 因此每个块有1个入口/行。在这些情况下有3行。

我试过这个,但似乎没有用。

perl -laF/\n/ `-000ne"print join chr(9),@F" myfile.txt`

3 个答案:

答案 0 :(得分:3)

perl -lanF"\n" -000 -e 'print join "\t", @F' file

答案 1 :(得分:1)

如果你不介意awk

$ awk -vRS= -vFS="\n" '{$1=$1}1' OFS="\t" file
1:SRX000566     Submitter: WoldLab      Study: RNASeq expression profiling for ENCODE project(SRP000228)        Sample: Human cell line GM12878(SRS000567)  Instrument: Solexa 1G Genome Analyzer    Total: 4 runs, 62.7M spots, 2.1G bases  Run #1: SRR002055, 11373440 spots, 375323520 bases      Run #2: SRR002063, 22995209 spots, 758841897 bases   Run #3: SRR005091, 13934766 spots, 459847278 bases      Run #4: SRR005096, 14370900 spots, 474239700 bases
2:SRX000565     Submitter: WoldLab      Study: RNASeq expression profiling for ENCODE project(SRP000228)        Sample: Human cell line GM12878(SRS000567)  Instrument: Solexa 1G Genome Analyzer    Total: 3 runs, 51.2M spots, 1.7G bases  Run #1: SRR002052, 12607931 spots, 416061723 bases      Run #2: SRR002054, 12880281 spots, 425049273 bases   Run #3: SRR002060, 25740337 spots, 849431121 bases
3:SRX012407     Submitter: GEO  Study: GSE17153: Illumina sequencing of small RNAs from C. elegans embryos(SRP001363)   Sample: Caenorhabditis elegans(SRS006961)    Instrument: Illumina Genome Analyzer II Total: 1 run, 3M spots, 106.8M bases    Run #1: SRR029428, 2965597 spots, 106761492 bases

否则等同于上述awk语句

#!/usr/bin/perl
$\ = "\n";
$/ = "\n\n";
while (<>) {
    chomp;
    @F = split(/\n/, $_);
    print join("\t",@F);
}

答案 2 :(得分:1)

将此视为正常的解析问题,并添加一点状态:

my @records;
my @current_record;

while( my $line = <> ) {
    chomp;

    if( length $line ) {
         # Store record data
         push @current_record, $line;
    }
    else {
        # Start new record
        push @records, [@current_record] if @current_record;
        @current_record = ();
    } 
}

print join "\t", @$_ for @records;

这是未经测试的,我需要去睡觉。如果它不起作用,我明天就要再看了。