2015-01-09 4 views
1

숫자 ID와 주석을 모두 포함하는 스프레드 시트에 열이 있습니다. 예를 들어perl : 하나의 열을 두 개로 나누십시오.

529120 30S ribosomal protein S3 
I 첫번째 열은 숫자 ID (529,120) 및 두 번째 칼럼을 포함하는 칼럼 둘로 분할하려는

는 주석 (30S 리보좀 단백질 S3)을 포함한다.

코드는 지금까지 첫 번째 열의 숫자 ID 만 인쇄 한 다음 종료됩니다. LIMIT = 2

#!/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"; 
} 

답변

4

split :

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 
+0

감사합니다! 이것을 새로운 tsv 파일에 어떻게 기록 할 수 있습니까? – Bex

+0

@Bex : 천만에. 출력을위한 파일을'열어 '파일 핸들을'print'합니다. – toolic

관련 문제