繁体   English   中英

perl:将一栏分为两部分

[英]perl: split one column into two

我在电子表格中有一列,其中既包含数字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";
}

以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

暂无
暂无

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

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