
grep
한 파일의 패턴을 두 번째 파일에서 검색하는 데 사용하고 싶습니다 . 내 패턴 파일은 다음과 같습니다.
K02217
K07448
KO8980
검색할 파일은 다음과 같습니다.
>aai:AARI_24510 proP; proline/betaine transporter; K03762 MFS transporter, MHS family, proline/betaine transporter
>aai:AARI_26600 ferritin-like protein; K02217 ferritin [EC:1.16.3.1]
>aai:AARI_28260 hypothetical protein
>aai:AARI_29060 ABC drug resistance transporter, inner membrane subunit; K09686 antibiotic transport system permease protein
>aai:AARI_29070 ABC drug resistance transporter, ATP-binding subunit (EC:3.6.3.-); K09687 antibiotic transport system ATP-binding protein
>aai:AARI_29650 hypothetical protein
>aai:AARI_32480 iron-siderophore ABC transporter ATP-binding subunit (EC:3.6.3.-); K02013 iron complex transport system ATP-binding protein [EC:3.6.3.34]
>aai:AARI_33320 mrr; restriction system protein Mrr; K07448 restriction system protein
내가 시도한 명령은 다음과 같습니다.
fgrep --file=pattern.txt file.txt >> output.txt
그러면 패턴이 발견된 file.txt 줄이 인쇄됩니다. 발견된 패턴이 있는 열도 인쇄하려면 필요합니다. 그래서 다음과 같습니다.
K07448 mrr; restriction system protein Mrr; K07448 restriction system
K02217 ferritin-like protein; K02217 ferritin [EC:1.16.3.1]
누구든지 나에게 수행 방법을 제안할 수 있습니까?
답변1
숫자가 포함된 추가 열이 마음에 들지 않으면 join
및 를 사용하여 grep
이 작업을 수행할 수 있습니다.
$ join <(grep -of patterns.txt file.txt | nl) \
<(grep -f patterns.txt file.txt | nl)
1 KO3322 proteinaseK (KO3322)
2 KO3435 Xxxxx KO3435;folding factor
3 KO3435 Yyyyy KO3435,xxxx
답변2
쉘 루프를 사용할 수 있습니다.
$ while read pat; do
grep "$pat" file |
while read match do
echo -e "$pat\t$match"
done
done < patterns
KO3435 Xxxxx KO3435;folding factor
KO3435 Yyyyy KO3435,xxxx
KO3322 proteinaseK (KO3322)
저는 이것을 인간용 UniProt 플랫 파일(625M)에서 실행하고 1000개의 UniProt ID를 패턴으로 사용하여 테스트했습니다. 내 Pentium i7 노트북에서는 약 6분이 걸렸습니다. 100개의 패턴만 찾는데 35초 정도 걸렸습니다.
아래 설명에서 지적한 대로 echo
및 를 건너뛰고 grep
's --label
및 -H
옵션을 사용하면 이 작업을 약간 더 빠르게 수행할 수 있습니다.
$ while read pat; do
grep "$pat" --label="$pat" -H < file
done < patterns
예제 파일에서 이를 실행하면 다음이 생성됩니다.
$ while read pat; do
grep "$pat" --label="$pat" -H < kegg.annotations;
done < allKO.IDs.txt > test1
terdon@oregano foo $ cat test1
K02217:>aai:AARI_26600 ferritin-like protein; K02217 ferritin [EC:1.16.3.1]
K07448:>aai:AARI_33320 mrr; restriction system protein Mrr; K07448 restriction system protein
답변3
당신이 사용할 수있는확인:
$ ack "$(tr '\n' '|' < pattern.txt | sed -e 's/.$//')" --print0 --output='$& $_' file.txt
KO3322 proteinaseK (KO3322)
KO3435 Xxxxx KO3435;folding factor
KO3435 Yyyyy KO3435,xxxx