<p>Assuming your sequences are always on a single line</p>
<pre><code>grep -B 1 &quot;^TA&quot; seqences.fasta | grep -v &quot;\-\-&quot; &gt; filtered_seqences.fasta
</code></pre>
<p>should do the job. Argument <code>-B</code> specifies how many lines before the pattern match should be printed. But there grep separates individual entries with <code>--</code> that's why I used a second grep to get rid of those.</p>
<p>Ha, apparently there is an undocumented <code>--no-group-separator</code> flag, so this should do as well</p>
<pre><code>grep -B 1 &quot;^TA&quot; seqences.fasta --no-group-separator &gt; filtered_seqences.fasta
</code></pre>

CC BY-SA 4.0
2020-09-16T16:08:48.130Z
GNU grep actually has an undocumented argument `--no-group-separator` that can do the trick too.
Jesse
CC BY-SA 4.0
2020-09-16T08:23:57.833Z
2020-09-16T17:02:27.253Z
2020-09-16T17:02:27.253Z
Kamil S Jaron
<p><a href="https://github.com/fhcrc/seqmagick" rel="nofollow noreferrer">seqmagick</a> has a handy <code>--seq-pattern-include</code> feature that works similarly to what Kamil did with a regular grep.</p>
<pre><code>$ cat sequences.fasta 
&gt;chr8:106028475-106028517()
GTaccaggcctagggttgcactcctgtagtctcagcattggg
&gt;chr8:106040430-106040472()
TAACCGGCTCTCCGCTGCCCTCATCTGTCTAGAATTCAGGTA
&gt;chr9:100245605-100245648()
TACCACCTCCCTCACTCTTTCCCAAATTTATGAGTTAGGCTAT

$ seqmagick convert --seq-pattern-include &quot;^TA&quot; sequences.fasta -
&gt;chr8:106040430-106040472()
TAACCGGCTCTCCGCTGCCCTCATCTGTCTAGAATTCAGGTA
&gt;chr9:100245605-100245648()
TACCACCTCCCTCACTCTTTCCCAAATTTATGAGTTAGGCTAT
</code></pre>
<p>It's a great little tool for this sort of thing built around Biopython, though it's been falling behind in compatibility with the latest versions as you can see from some of the open GitHub issues.</p>

CC BY-SA 4.0
2020-09-16T16:13:42.460Z
2020-09-16T16:13:42.460Z
Jesse
<p>I have a fasta file and the sequences in them are arranged like this:</p>
<pre><code>&gt;chr8:106028475-106028517()
GTaccaggcctagggttgcactcctgtagtctcagcattggg
&gt;chr8:106040430-106040472()
TAACCGGCTCTCCGCTGCCCTCATCTGTCTAGAATTCAGGTA
&gt;chr9:100245605-100245648()
TACCACCTCCCTCACTCTTTCCCAAATTTATGAGTTAGGCTAT
</code></pre>
<p>I want to extract only those which begin with TA. so my final output should look like</p>
<pre><code>&gt;chr8:106040430-106040472()
TAACCGGCTCTCCGCTGCCCTCATCTGTCTAGAATTCAGGTA
&gt;chr9:100245605-100245648()
TACCACCTCCCTCACTCTTTCCCAAATTTATGAGTTAGGCTAT
</code></pre>
<p>Kindly help.</p>

CC BY-SA 4.0
2020-09-15T14:23:09.520Z
Hi. People won't write your code for your (especially as a bit of googling leads to some very simple one-liners which do what you need), but they will help you if you show what code you have already tried to write. Thanks.
user438383
<fasta><sequence>
get sequences begining with TA
# get sequences begining with TA

(Asked by: username_0 on 2020-09-15)

I have a fasta file and the sequences in them are arranged like this:
>chr8:106028475-106028517()
GTaccaggcctagggttgcactcctgtagtctcagcattggg
>chr8:106040430-106040472()
TAACCGGCTCTCCGCTGCCCTCATCTGTCTAGAATTCAGGTA
>chr9:100245605-100245648()
TACCACCTCCCTCACTCTTTCCCAAATTTATGAGTTAGGCTAT

I want to extract only those which begin with TA. so my final output should look like
>chr8:106040430-106040472()
TAACCGGCTCTCCGCTGCCCTCATCTGTCTAGAATTCAGGTA
>chr9:100245605-100245648()
TACCACCTCCCTCACTCTTTCCCAAATTTATGAGTTAGGCTAT

Kindly help.

**Tags:** fasta, sequence
> **Comments:**
> **username_1**: Hi. People won't write your code for your (especially as a bit of googling leads to some very simple one-liners which do what you need), but they will help you if you show what code you have already tried to write. Thanks.  

---

## 2 answers

### Answer by username_3 on 2020-09-16. Score: 1

seqmagick has a handy --seq-pattern-include feature that works similarly to what Kamil did with a regular grep.
$ cat sequences.fasta 
>chr8:106028475-106028517()
GTaccaggcctagggttgcactcctgtagtctcagcattggg
>chr8:106040430-106040472()
TAACCGGCTCTCCGCTGCCCTCATCTGTCTAGAATTCAGGTA
>chr9:100245605-100245648()
TACCACCTCCCTCACTCTTTCCCAAATTTATGAGTTAGGCTAT

$ seqmagick convert --seq-pattern-include "^TA" sequences.fasta -
>chr8:106040430-106040472()
TAACCGGCTCTCCGCTGCCCTCATCTGTCTAGAATTCAGGTA
>chr9:100245605-100245648()
TACCACCTCCCTCACTCTTTCCCAAATTTATGAGTTAGGCTAT

It's a great little tool for this sort of thing built around Biopython, though it's been falling behind in compatibility with the latest versions as you can see from some of the open GitHub issues.


---

### Answer by username_2 on 2020-09-16. Score: 0

Assuming your sequences are always on a single line
grep -B 1 "^TA" seqences.fasta | grep -v "\-\-" > filtered_seqences.fasta

should do the job. Argument -B specifies how many lines before the pattern match should be printed. But there grep separates individual entries with -- that's why I used a second grep to get rid of those.
Ha, apparently there is an undocumented --no-group-separator flag, so this should do as well
grep -B 1 "^TA" seqences.fasta --no-group-separator > filtered_seqences.fasta

> **Comments:**
> **username_3**: GNU grep actually has an undocumented argument `--no-group-separator` that can do the trick too.  

---

