2012-02-01 2 views
0

하나 개의 텍스트 파일을문자열 일치 검색

fooLONGcite 
GetmoreDATA 
stringMATCH 
GOODthing 

이 같은 대상 파일과 같은 다른 텍스트 파일 :

sometingfooLONGcite 
anyotherfooLONGcite 
matchGetmoreDATA 
GETGOODthing 
brotherGETDATA 
CITEMORETHING 
TOOLONGSTUFFETC 

예상됩니다 할 대상 파일에서 일치하는 문자열을 얻을 그런 다음 인쇄하십시오. 출력은 다음과 같아야합니다.

sometingfooLONGcite 
anyotherfooLONGcite 
matchGetmoreDATA  
GETGOODthing 

여기 내 필용 스크립트입니다. 그러나 그것은 작동하지 않습니다. 문제가있는 곳을 찾도록 도와 줄 수 있습니까? 감사.

#!/usr/bin/perl 
use strict; 

# to check the command line option 
if($#ARGV<0){ 
    printf("Usage: \n <tag> <seq> <outfile>\n"); 
    exit 1; 
} 

# to open the given infile file 
open(tag, $ARGV[0]) or die "Cannot open the file $ARGV[0]"; 
open(seq, $ARGV[1]) or die "Cannot open the file $ARGV[1]"; 

my %seqhash =(); 
my $tag_id; 
my $tag_seq; 
my $seq_id; 
my $seq_seq; 
my $seq; 
my $i = 0; 

print "Processing cds seq\n"; 
#check the seq file 
while(<seq>){ 
    my @line = split; 
    if($i != 0){ 
     $seqhash{$seq_seq} = $seq; 
     $seq = ""; 
     print "$seq_seq\n"; 
    } 
    $seq_seq = $line[0]; 
    $i++; 
} 

while(<tag>){ 
    my @tagline = split; 
    $tag_seq = $tagline[0]; 
    $seq = $seqhash{$seq_seq}; 
    #print "$tag_seq\n"; 
    print "$seq\n"; 
    #print output ">$id\n$seq\n"; 
} 
#print "Ending of Processing gff\n"; 

close(tag); 
close(seq); 
+1

:

perl script.pl query.txt subject.txt 

그리고 결과 :

use warnings; use strict; ## Check arguments. die qq[Usage: perl $0 <query_file> <subject_file>\n] unless @ARGV == 2; ## Open input files. Abort if found errors. open my $fh_query, qq[<], shift @ARGV or die qq[Cannot open input file: $!\n]; open my $fh_subject, qq[<], shift @ARGV or die qq[Cannot open input file: $!\n]; ## Variable to save a regex with alternations of the content of the 'query' file. my $query_regex; { ## Read content of the 'query' file in slurp mode. local $/ = undef; my $query_content = <$fh_query>; ## Remove trailing spaces and generate a regex. $query_content =~ s/\s+\Z//; $query_content =~ s/\n/|/g; $query_regex = qr/(?i:($query_content))/; } ## Read 'subject' file and for each line compare if that line matches with ## any word of the 'query' file and print in success. while (<$fh_subject>) { if (m/$query_regex/o) { print } } 

스크립트를 실행 내가 정규식에 모든 콘텐츠를 추가하기 때문에 쿼리 파일이 작다는 사실을 고려 당신은 했습니까?] (http://mattgemmell.com/2008/12/08/what-have-you-tried/) –

+0

내 스크립트를 추가했습니다. – Jianguo

답변

1

제가 알고 있듯이 정확한 문자열이 아닌 문자열의 일부를 찾습니다. 여기 내가 찾고 있다고 생각하는 것을 수행하는 스크립트 :

콘텐츠가 script.pl입니다. [이 무엇

sometingfooLONGcite 
anyotherfooLONGcite 
matchGetmoreDATA 
GETGOODthing 
+0

잘 작동합니다. 그러나 다른 파일을 사용하면 작동하지 않습니다. 고칠 수있게 도와 주실 수 있습니까? 감사. 다음은 새 데이터 링크입니다. http : //stackoverflow.com/questions/9101082/extract-sequence-information-using-tag-sequence – Jianguo

0

현재 코드는별로 의미가 없습니다. 당신은 아무 것도 지정하지 않은 변수를 참조 할 수도 있습니다.

첫 번째 파일을 해시로 읽은 다음 두 번째 행에서 해당 해시를 확인하기 만하면됩니다.

while (my $line = <FILE>) 
{ 
    chomp($line); 
    $hash{$line} = 1; 
} 

... 

while (my $line = <FILE2>) 
{ 
    chomp($line); 
    if (defined $hash{$line}) 
    { 
     print "$line\n"; 
    } 
} 
+0

이 코드를 실행했는데 왜 아무 일도 없었습니다. 고마워. – Jianguo

+0

:: sigh :: 왜냐하면 그것은 단지 당신이해야 할 일의 예이기 때문입니다. –

+0

이 코드를 완성하도록 도와 줄 수 있습니까? 부디. 나는 펄에서 아주 새로운 사람이다. 도와 줘서 고마워. – Jianguo