[질문] 두개의 파일에서 공통된 data만 뽑아내기.

junsa의 이미지

두개의 파일이 있습니다.

한개는

id
A08031722
B09032408
A04040624
B02022419
B07032311
B11011914
B04042308
B04012325
.
.
.

이런 식의 모양을 하고있는 파일이고 다른 한개는

>AAA|ss000100_01-1-A14_A11010102
podjfieohjgfsnfvgasldmdovieiosieogoigkdaklk
>AAA|ss000099_01-1-A12_A01010101
sosdkfirejghrfidpsagpqwpwedjfjdskfngvakjfdk
.
.
이런 모양을 하고있습니다.
첫번째 파일의 id와 두번째 파일의 굵게 표시된 글씨가 match되는
data만 뽑아내서

>A11010102
podjfieohjgfsnfvgasldmdovieiosieogoigkdaklk

이런 모양으로 data를 출력하고 싶습니다.
pattern을 match시켜 data를 sorting하는 일인데
생각만큼 만만치가 않아서 도움을 청합니다.
파일내의 data수가 한두개가 아니니 일일이 눈으로 맞춰볼수도 없는일이고....ㅜㅜ
정규표현식, perl, python 수준에서 해결할수 있었으면 합니다.
(사실 다른 언어는 잘 몰라서... ㅡㅡ;;)

고수님들의 프로그래밍 능력을 보여주세여^^
부탁드립니다 꾸벅...!!

wfellow의 이미지

구글신에게 BM에 대한 논문을 부탁해 보시져. 보이어-무어의 논문에 관련된 내용이 있을것입니다. 인터넷 보안장비의 내부에서 사용하는 매칭기법의 기초입니다만 질문조건에 딱 맞는 해결방법일것입니다.

ps: 전에 저는 유료사이트에서 파일을 받았다는 기억이... 지금은 찾지를 못하겠네요.

-----[꼬릿말 절취선 시작]-----
삽질전에 먼저 구글신께 기도하자.
-----[꼬릿말 절취선 끝]-----

wpcasper의 이미지

#!/usr/bin/perl
open(FH, "<file1");
@file1 = <FH>;
close(FH);
#index를 읽어서 배열로 만듬.


open(FH, "<file2");
@file2 = <FH>;
close(FH);

my $file2data;

foreach $temp (@file2) {
  $file2data = $file2data."\n".$temp;
}
#두번째 파일내용을 통채로 변수에 저장.


my $trash;

foreach $id (@file1) {
  if($file2data =~ m/$id\n(.*?)\n/) {
    $trash = $trash.'>'.$id.$1."\n";
  }
}

print $trash;

이러면 대략 될것 같은데요.
첫번째 파일 맨위에 id 지우시고, 두번째 파일 맨 마지막에 엔터한번 쳐주시면 될겁니다.

wpcasper의 이미지

그나저나 bioinfomatics 하시나요?

junsa의 이미지

답변 정말 큰 도움이 되었습니다!! 감솨함돠!!

예... 저는 bioinformatics를 공부하고 있는 학생임니다.

생물학과 프로그래밍 공부를 같이 하다보니 아무래도 버겁네여^^;:

저 그런데 match되는 id는 sorting되지만 다음줄에 영문이

출력되지 않습니다.

이것저것 고쳐봤는데 진전이 없네여

혹시 뭐가 잘못된것지....ㅜㅜ

한번 더 부탁드립니다.

wake up!!

wpcasper의 이미지

file 1과 file 2에서 각 행의 마지막에 공백의 유/무가 달라서 생기는 현상입니다. 공백은 별로 의미가 없을 것이기에 공백을 없애버리는 명령을 추가했습니다. 파스타 맞나요? 물리화학 교수님께서 숙제로 내주신 추억에. 덜덜덜..

#!/usr/bin/perl 
open(FH, "<file1"); 
@file1 = <FH>; 
close(FH); 
#index를 읽어서 배열로 만듬. 


open(FH, "<file2"); 
@file2 = <FH>; 
close(FH); 

my $file2data; 

foreach $temp (@file2) { 
  $file2data = $file2data."\n".$temp; 
} 
#두번째 파일내용을 통채로 변수에 저장. 

$file2data =~ s/ //g;
#요거랑

my $trash; 

foreach $id (@file1) { 
    $id =~ s/ //g;
    #요 부분입니다.
    
    if($file2data =~ m/$id\n(.*?)\n/) { 
        $trash = $trash.'>'.$id.$1."\n"; 
    } 
} 

print $trash;

아마 잘될겁니다. 혹시 잘 안되면 파일을 올려주세요.
이미 보시고 계시겠지만. O' Reilly 에서 나온 bioinfomatics 와 Mastering Regular Expressions 추천합니다.

쌀밥의 이미지

사 놓고 안보고 있는 'perl로 시작하는 바이오 인포메틱스' 책을 가지고 있습니다.

혹시 도움이 되실지도 모르니 서점에 가시면 들춰보세요..;

일하는 사람들의 희망 민주노동당 : http://www.kdlp.org
반공 교육의 성과로, 민주주의의 반대가 공산주의(또는 사회주의)라고 생각하는 사람이 많다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.