<p>Here's one way:</p>

<pre><code>$ sed -E 's/&gt;[^:]*::([^:]*)::.*/&gt;\1/' file.fa 
&gt;TRINITY_DN100000_c1_g1_i3
MVWIKFRGLHRVLTSTPLVKSGKTPSQTWAFLDISVELIVFLFLNVHKSPMPHFKIYSEA
FSEEWSLLWLQYSRHLIQKPKPWQIKIELLHLCCCNRLC*
&gt;TRINITY_DN100000_c1_g6_i2
MEMMQEIIPFAREMLSARPSKGTMKVYLVGGTFAVLGIVSGMVEAACSLFPEQEESTLTK
LMEDCLTVTAQNQEPQTFIPEDDEQDAEMEAKAKDLPMFRQRRMSFRAHAS*
</code></pre>

<p>The idea is to match a <code>&gt;</code>, then as many non<code>:</code> as possible (<code>[^:]*</code>), and then capture the string between the first <code>::</code> and the second <code>::</code>. Finally, you replace the entire line with the captured string. </p>

<p>Once you're satisfied this does what you need, you can edit the original file with <code>-i</code>:</p>

<pre><code>sed -i -E 's/&gt;[^:]*::([^:]*)::.*/&gt;\1/' file.fa 
</code></pre>

<hr>

<p>Alternatively, set <code>awk</code>'s field separator to <code>::</code> and print the second field only on lines that start with <code>&gt;</code>:</p>

<pre><code><span class="math-container">$ awk -F'::' '/^&gt;/ ? $</span>0="&gt;"$2 : 1' file.fa 
&gt;TRINITY_DN100000_c1_g1_i3
MVWIKFRGLHRVLTSTPLVKSGKTPSQTWAFLDISVELIVFLFLNVHKSPMPHFKIYSEA
FSEEWSLLWLQYSRHLIQKPKPWQIKIELLHLCCCNRLC*
&gt;TRINITY_DN100000_c1_g6_i2
MEMMQEIIPFAREMLSARPSKGTMKVYLVGGTFAVLGIVSGMVEAACSLFPEQEESTLTK
LMEDCLTVTAQNQEPQTFIPEDDEQDAEMEAKAKDLPMFRQRRMSFRAHAS*
</code></pre>

CC BY-SA 4.0
2019-10-07T09:27:53.767Z
2019-10-07T15:56:40.913Z
2019-10-07T15:56:40.913Z
terdon
<p>A 2 step workaround is:</p>

<ol>
<li><p>Get rid of everything after the piece of info you want, that is anything starting with <code>::</code> and goes on with <code>g</code>:</p>

<p><code>sed "s/::g.*//" fasta &gt; fasta2</code></p></li>
</ol>

<p><code>&gt;TRINITY_DN100000_c1_g1::TRINITY_DN100000_c1_g1_i3</code></p>

<ol start="2">
<li><p>Get rid of the "first header" as you call it, after the first step, it is rather straightforward:</p>

<p><code>sed "s/&gt;TRINITY_.*::/&gt;/" fasta2 &gt; fasta3</code></p></li>
</ol>

<p><code>&gt;TRINITY_DN100000_c1_g1_i3</code></p>

<p>EDIT:</p>

<p>@terdon showed that multiple <code>sed</code> commands can be used when separated by <code>;</code> in GNU <code>sed</code> (default on Linux):</p>

<pre><code>sed "s/::g.*//; s/&gt;TRINITY_.*::/&gt;/" fasta &gt; modified_fasta
</code></pre>

CC BY-SA 4.0
2019-10-06T22:46:11.543Z
it worked!! thank you!
lili
CC BY-SA 4.0
2019-10-07T09:30:13.487Z
There is no need for multiple steps: `sed 's/::g.*//; s/>TRINITY_.*::/>/'`. Or, if you don't have GNU `sed`: `sed -e 's/::g.*//' -e 's/>TRINITY_.*::/>/'`. You could even pipe one `sed` into another, that would at least avoid the temp files: `sed 's/::g.*//' | 
 sed 's/>TRINITY_.*::/>/'`.
terdon
CC BY-SA 4.0
2019-10-06T21:39:33.333Z
2019-10-07T11:16:48.133Z
2019-10-07T11:16:48.133Z
haci
<p>I have a fasta file, like this:</p>

<pre><code>&gt;TRINITY_DN100000_c1_g1::TRINITY_DN100000_c1_g1_i3::g.3039::m.3039 TRINITY_DN100000_c1_g1::TRINITY_DN100000_c1_g1_i3::g.3039  ORF type:complete len:100 (-) TRINITY_DN100000_c1_g1_i3:1027-1326(-)
MVWIKFRGLHRVLTSTPLVKSGKTPSQTWAFLDISVELIVFLFLNVHKSPMPHFKIYSEA
FSEEWSLLWLQYSRHLIQKPKPWQIKIELLHLCCCNRLC*
&gt;TRINITY_DN100000_c1_g6::TRINITY_DN100000_c1_g6_i2::g.84365::m.84365 TRINITY_DN100000_c1_g6::TRINITY_DN100000_c1_g6_i2::g.84365  ORF type:complete len:112 (-) TRINITY_DN100000_c1_g6_i2:379-714(-)
MEMMQEIIPFAREMLSARPSKGTMKVYLVGGTFAVLGIVSGMVEAACSLFPEQEESTLTK
LMEDCLTVTAQNQEPQTFIPEDDEQDAEMEAKAKDLPMFRQRRMSFRAHAS*
</code></pre>

<p>if I want to only keep the second header, like this (the amino acid sequence keep unchanged). which command could i USE?</p>

<pre><code>&gt;TRINITY_DN100000_c1_g1_i3
MVWIKFRGLHRVLTSTPLVKSGKTPSQTWAFLDISVELIVFLFLNVHKSPMPHFKIYSEA
FSEEWSLLWLQYSRHLIQKPKPWQIKIELLHLCCCNRLC*
&gt;TRINITY_DN100000_c1_g6_i2
MEMMQEIIPFAREMLSARPSKGTMKVYLVGGTFAVLGIVSGMVEAACSLFPEQEESTLTK
LMEDCLTVTAQNQEPQTFIPEDDEQDAEMEAKAKDLPMFRQRRMSFRAHAS*
</code></pre>

<p>I know a command </p>

<pre><code>&gt; sed 's/::.*//' input &gt; output 
</code></pre>

<p>can only keep the first header <code>&gt;TRINITY_DN100000_c1_g1</code> if I want to keep the second header with the isoform information <code>&gt;TRINITY_DN100000_c1_g1_i3</code>, how should I correct this command?</p>

CC BY-SA 4.0
2019-10-07T09:37:58.037Z
You can also do this via eFetch or Biopython directly without regex-ing
M__
<rna-seq>
too long header for fasta file
# too long header for fasta file

(Asked by: username_0 on 2019-10-06)

I have a fasta file, like this:
>TRINITY_DN100000_c1_g1::TRINITY_DN100000_c1_g1_i3::g.3039::m.3039 TRINITY_DN100000_c1_g1::TRINITY_DN100000_c1_g1_i3::g.3039  ORF type:complete len:100 (-) TRINITY_DN100000_c1_g1_i3:1027-1326(-)
MVWIKFRGLHRVLTSTPLVKSGKTPSQTWAFLDISVELIVFLFLNVHKSPMPHFKIYSEA
FSEEWSLLWLQYSRHLIQKPKPWQIKIELLHLCCCNRLC*
>TRINITY_DN100000_c1_g6::TRINITY_DN100000_c1_g6_i2::g.84365::m.84365 TRINITY_DN100000_c1_g6::TRINITY_DN100000_c1_g6_i2::g.84365  ORF type:complete len:112 (-) TRINITY_DN100000_c1_g6_i2:379-714(-)
MEMMQEIIPFAREMLSARPSKGTMKVYLVGGTFAVLGIVSGMVEAACSLFPEQEESTLTK
LMEDCLTVTAQNQEPQTFIPEDDEQDAEMEAKAKDLPMFRQRRMSFRAHAS*

if I want to only keep the second header, like this (the amino acid sequence keep unchanged). which command could i USE?
>TRINITY_DN100000_c1_g1_i3
MVWIKFRGLHRVLTSTPLVKSGKTPSQTWAFLDISVELIVFLFLNVHKSPMPHFKIYSEA
FSEEWSLLWLQYSRHLIQKPKPWQIKIELLHLCCCNRLC*
>TRINITY_DN100000_c1_g6_i2
MEMMQEIIPFAREMLSARPSKGTMKVYLVGGTFAVLGIVSGMVEAACSLFPEQEESTLTK
LMEDCLTVTAQNQEPQTFIPEDDEQDAEMEAKAKDLPMFRQRRMSFRAHAS*

I know a command 
> sed 's/::.*//' input > output 

can only keep the first header >TRINITY_DN100000_c1_g1 if I want to keep the second header with the isoform information >TRINITY_DN100000_c1_g1_i3, how should I correct this command?

**Tags:** rna-seq
> **Comments:**
> **username_1**: You can also do this via eFetch or Biopython directly without regex-ing  

---

## 2 answers

### Answer by username_2 on 2019-10-07. Score: 2

Here's one way:
$ sed -E 's/>[^:]*::([^:]*)::.*/>\1/' file.fa 
>TRINITY_DN100000_c1_g1_i3
MVWIKFRGLHRVLTSTPLVKSGKTPSQTWAFLDISVELIVFLFLNVHKSPMPHFKIYSEA
FSEEWSLLWLQYSRHLIQKPKPWQIKIELLHLCCCNRLC*
>TRINITY_DN100000_c1_g6_i2
MEMMQEIIPFAREMLSARPSKGTMKVYLVGGTFAVLGIVSGMVEAACSLFPEQEESTLTK
LMEDCLTVTAQNQEPQTFIPEDDEQDAEMEAKAKDLPMFRQRRMSFRAHAS*

The idea is to match a >, then as many non: as possible ([^:]*), and then capture the string between the first :: and the second ::. Finally, you replace the entire line with the captured string. 
Once you're satisfied this does what you need, you can edit the original file with -i:
sed -i -E 's/>[^:]*::([^:]*)::.*/>\1/' file.fa 


Alternatively, set awk's field separator to :: and print the second field only on lines that start with >:
$ awk -F'::' '/^>/ ? $0=">"$2 : 1' file.fa 
>TRINITY_DN100000_c1_g1_i3
MVWIKFRGLHRVLTSTPLVKSGKTPSQTWAFLDISVELIVFLFLNVHKSPMPHFKIYSEA
FSEEWSLLWLQYSRHLIQKPKPWQIKIELLHLCCCNRLC*
>TRINITY_DN100000_c1_g6_i2
MEMMQEIIPFAREMLSARPSKGTMKVYLVGGTFAVLGIVSGMVEAACSLFPEQEESTLTK
LMEDCLTVTAQNQEPQTFIPEDDEQDAEMEAKAKDLPMFRQRRMSFRAHAS*


---

### Answer by username_3 on 2019-10-06. Score: 1

A 2 step workaround is:

Get rid of everything after the piece of info you want, that is anything starting with :: and goes on with g:
sed "s/::g.*//" fasta > fasta2

>TRINITY_DN100000_c1_g1::TRINITY_DN100000_c1_g1_i3

Get rid of the "first header" as you call it, after the first step, it is rather straightforward:
sed "s/>TRINITY_.*::/>/" fasta2 > fasta3

>TRINITY_DN100000_c1_g1_i3
EDIT:
@terdon showed that multiple sed commands can be used when separated by ; in GNU sed (default on Linux):
sed "s/::g.*//; s/>TRINITY_.*::/>/" fasta > modified_fasta

> **Comments:**
> **username_0**: it worked!! thank you!  
> **username_2**: There is no need for multiple steps: `sed 's/::g.*//; s/>TRINITY_.*::/>/'`. Or, if you don't have GNU `sed`: `sed -e 's/::g.*//' -e 's/>TRINITY_.*::/>/'`. You could even pipe one `sed` into another, that would at least avoid the temp files: `sed 's/::g.*//' |   sed 's/>TRINITY_.*::/>/'`.  

---

