<p>Use this Perl one-liner:</p>
<pre><code>perl -lne '$id = $1 if /^&gt;(.+)/; ($m) = /(CC[GT]AAA[GC][AC]TT[GC])/; print join &quot;\t&quot;, $id, $m if $m;' input.fasta
</code></pre>
<p>The Perl one-liner uses these command line flags:<br />
<code>-e</code> : Tells Perl to look for code in-line, instead of in a file.<br />
<code>-n</code> : Loop over the input one line at a time, assigning it to <code>$_</code> by default.<br />
<code>-l</code> : Strip the input line separator (<code>&quot;\n&quot;</code> on *NIX by default) before executing the code in-line, and append it when printing.</p>
<p><strong>SEE ALSO:</strong><br />
<a href="https://perldoc.perl.org/perlrun.html#Command-Switches" rel="nofollow noreferrer"><code>perldoc perlrun</code>: how to execute the Perl interpreter: command line switches</a></p>

CC BY-SA 4.0
2021-01-17T21:40:42.950Z
2021-01-17T21:40:42.950Z
Timur Shtatland
<p>I have a file containing multiple fast sequneces. For a specific consensus pattern as input, I extracted all the matching patterns from target fasta sequences with</p>
<pre><code>grep -o -E &quot;CC[GT]AAA[GC][AC]TT[GC]&quot; input.fasta
</code></pre>
<p>However, the above command will retrieve just the matching sequences from fasta sequences and <strong>I  also wanted to get the corresponding fasta header of each match</strong>.</p>
<p>For example, if <strong>input.fasta</strong>  file is something like this,</p>
<pre><code>&gt;Gene 1
TGATGAAAAATGATAGAT
ATTGGGGGAAAAAAAAAT

&gt;Gene 2
TTTCCTAAAGATTGT
AAATTTAAAAATGTTTTT
</code></pre>
<p>(Gene 2 has matching subsequence CCTAAAGATTG)</p>
<p>Output:</p>
<pre><code>CCTAAAGATTG   Gene2
</code></pre>
<p>I prefer a solution with grep. But other possible solutions also helpful.</p>

<fasta><motifs><grep>
How to get matching pattern along with ID in a single command in grep?
# How to get matching pattern along with ID in a single command in grep?

(Asked by: username_0 on 2021-01-17)

I have a file containing multiple fast sequneces. For a specific consensus pattern as input, I extracted all the matching patterns from target fasta sequences with
grep -o -E "CC[GT]AAA[GC][AC]TT[GC]" input.fasta

However, the above command will retrieve just the matching sequences from fasta sequences and I  also wanted to get the corresponding fasta header of each match.
For example, if input.fasta  file is something like this,
>Gene 1
TGATGAAAAATGATAGAT
ATTGGGGGAAAAAAAAAT

>Gene 2
TTTCCTAAAGATTGT
AAATTTAAAAATGTTTTT

(Gene 2 has matching subsequence CCTAAAGATTG)
Output:
CCTAAAGATTG   Gene2

I prefer a solution with grep. But other possible solutions also helpful.

**Tags:** fasta, motifs, grep

---

## 1 answers

### Answer by username_1 on 2021-01-17. Score: 1 | Accepted answer

Use this Perl one-liner:
perl -lne '$id = $1 if /^>(.+)/; ($m) = /(CC[GT]AAA[GC][AC]TT[GC])/; print join "\t", $id, $m if $m;' input.fasta

The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-n : Loop over the input one line at a time, assigning it to $_ by default.
-l : Strip the input line separator ("\n" on *NIX by default) before executing the code in-line, and append it when printing.
SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches


---

