
다음과 같은 파일이 있습니다(5개의 탭으로 구분된 열).
head allKO.txt
Metabolism Carbohydrate metabolism Glycolisis K07448
Metabolism Protein metabolism protesome K02217
파일의 5열에서 패턴(문자열)을 검색하고 KEGG.annotations
, 패턴이 발견되면 KEGG.annotations
패턴이 발견된 줄과 의 모든 열을 모두 다른 파일에 인쇄하고 싶습니다 allKO.txt
. 패턴을 찾고 있는 파일은 다음과 같습니다.
head KEGG.annotations
>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
나는 다음과 같은 것을 원합니다 :
Metabolism Carbohydrate metabolism Glycolisis K07448 >aai:AARI_33320 mrr; restriction system protein Mrr; K07448 restriction system
Metabolism Protein metabolism proteasome K02217 >aai:AARI_26600 ferritin-like protein; K02217 ferritin [EC:1.16.3.1]
>aai:AARI_33320 mrr; restriction …
첫 번째 줄에 추가되는 텍스트 는 의 8번째 줄이며 KEGG.annotations
, 이는 의 K07448
첫 번째 줄의 ID 필드(다섯 번째 필드)를 포함하는 것입니다 allKO.txt
.
내 패턴 파일을 사용하려면 이 코드를 어떻게 수정해야 합니까? 이는 찾을 특정 패턴을 포함하는 열이 하나만 있는 패턴 파일에서 작동합니다.
while read pat; do
grep "$pat" --label="$pat" -H < KEGG.annotations;
done < allKO.txt > test1
답변1
이미 가지고 있는 코드로 작업할 수 있습니다. 행을 배열에 저장하고 다섯 번째 요소와 일치시킵니다.
while read -r line; do
[ -z "$line" ] && continue
patlist=($line)
pat=${patlist[4]}
grep "$pat" --label="$line" -H < KEGG.annotations
done < allKO.txt
보고:
Metabolism Carbohydrate metabolism Glycolisis K07448:>aai:AARI_33320 mrr; restriction system protein Mrr; K07448 restriction system protein
Metabolism Protein metabolism protesome K02217:>aai:AARI_26600 ferritin-like protein; K02217 ferritin [EC:1.16.3.1]
답변2
이것은 당신이 요구하는 것 같습니다 :
while read w1 w2 w3 w4 ID
do
printf "%s " "$w1 $w2 $w3 $w4 $ID"
if ! grep "$ID" KEGG.annotations
then
echo
fi
done < allKO.txt
그러면 화면에 출력이 기록됩니다. 출력( >
) 리디렉션(예: > test1
)을 마지막 줄에 추가하여 출력을 파일로 캡처합니다.
- 귀하의 예에 따르면 키/ID 필드("패턴")는다섯~의다섯필드가 파일에 있으므로
allKO.txt
우리는read w1 w2 w3 w4 ID
. 당신은 이것이 탭으로 구분된 파일이라고 말했습니다. 필드에 공백이 포함되어 있지 않다고 가정합니다. printf
에서 줄(즉, 필드)을 작성합니다allKO.txt
. 끝에는 공백이 있지만 줄바꿈은 끝나지 않습니다.- 파일에서 ID( 의 줄에서 다섯 번째 필드 )를 검색(
grep
) 합니다 . 이는 완전한 라인(개행 포함)입니다.KEGG.annotations
allKO.txt
- 실패 하면
grep
실패했으므로 개행 문자를 작성하십시오printf
. 그러면 ID가 없는 행이
KEGG.annotations
출력에 간단히 기록됩니다.Metabolism Protein metabolism proteasome K02217 >aai:AARI_26600 ferritin-like protein; K02217 ferritin [EC:1.16.3.1] This ID doesn’t exist: K99999
두 번 이상 존재하는 ID는 추가 줄로 기록됩니다(의 데이터를 반복하지 않음
allKO.txt
).Metabolism Protein metabolism proteasome K02217 >aai:AARI_26600 ferritin-like protein; K02217 ferritin [EC:1.16.3.1] This is a hypothetical additional line from KEGG.annotations that mentions “K02217”.