I want to use grep to search pattern from one file in a second one.My pattern file is something like:K02217K07448KO8980The file to search in is:>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 proteinThe command I tried is:fgrep --file=pattern.txt file.txt >> output.txtThis prints the lines of file.txt where the pattern is found. I need it to print also a column with the pattern found. So something like:K07448 mrr; restriction system protein Mrr; K07448 restriction systemK02217 ferritin-like protein; K02217 ferritin [EC:1.16.3.1]Anyone can suggest me how to do?
Grep for searching pattern in a file
text processing;grep
You can use a shell loop:$ while read pat; do     grep $pat file |         while read match do             echo -e $pat\t$match        done done < patterns KO3435  Xxxxx KO3435;folding factorKO3435  Yyyyy KO3435,xxxxKO3322  proteinaseK (KO3322)I tested by running this on the UniProt flat file for human (625M) and using 1000 UniProt IDs as the patterns. It took ~6 minutes on my Pentium i7 laptop. It took ~35 seconds when I only looked for 100 patterns. As pointed out in the comments below, you can make this slightly faster by skipping the echo and using grep's --label and -H options:$ while read pat; do     grep $pat --label=$pat -H < filedone < patternsRunning this on your example files produces:$ while read pat; do     grep $pat --label=$pat -H < kegg.annotations;   done < allKO.IDs.txt > test1terdon@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