perl:将一列拆分为两列

时间:2015-01-09 21:15:01

标签: regex perl split

我在电子表格中有一个列,其中包含数字ID和注释。例如:

529120  30S ribosomal protein S3

我希望将该列分成两列,其中第一列包含数字ID(529120),第二列包含注释(30S核糖体蛋白S3)。

到目前为止,我的代码只打印出第一列的数字ID,然后终止。

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

my $annotationsFile = "/Users/mycomputer/Desktop/AnnotationsSplit.tsv";

    open( ANNOTATIONS, "<", $annotationsFile )
      or die "Cannot open file $!";

      while ( my $line = <ANNOTATIONS> ) {
        chomp $line;
        my @column     = split( /\t/, $line );
        my $annotationFull = $column[3];
        my ($annotationNumber) = $annotationFull =~ (/^(\d+)/);
        print $annotationNumber, "\n";
}

1 个答案:

答案 0 :(得分:4)

split,LIMIT = 2:

use warnings;
use strict;

while (my $line = <DATA>) {
    chomp $line;
    my ($id, $annot) = split /\s+/, $line, 2;
    print "id    = $id\n";
    print "annot = $annot\n";
}

__DATA__
529120 30S ribosomal protein S3

输出:

id    = 529120
annot = 30S ribosomal protein S3