<p>The problem is in the <code>grep</code> command you're using, which only sends the fasta file headers to the output. Here's my guess at what the commands in your pipeline do:</p>
<ol>
<li><p><code>grep '^&gt;' 001_old.fasta</code> - extract lines starting with '&gt;' from <code>001_old.fasta</code></p>
</li>
<li><p><code>cut -d ' ' -f 1,8</code> - assume file contains space-delimited records; extract only the first and eighth field from each line</p>
</li>
<li><p><code>sed '/^&gt;/s/$/_001/'</code> - in lines that begin with a '&gt;', replace the end of the line with '_001'</p>
</li>
<li><p><code>&gt; 001_new.fasta</code> - write result out to <code>001_new.fasta</code></p>
</li>
</ol>
<p>Of all of these, I think only the <code>sed</code> and output parts are important, but it needs to be slightly modified to work as intended:</p>
<pre><code>sed '/^&gt;/s/ .*$/_001/' 001_old.fasta &gt; 001_new.fasta
</code></pre>
<p>Assuming I've got the syntax right, this should search for lines beginning with '&gt;', and replace everything from the first space up to the end of the line with '_001'.</p>
<p>I'm more familiar with perl, so here's a perl one-liner that should do the same thing:</p>
<pre><code>perl -pe '/^&gt;/ &amp;&amp; s/ .*$/_001/' 001_old.fasta &gt; 001_new.fasta
</code></pre>

CC BY-SA 4.0
2023-06-28T18:54:47.957Z
thank you so much! this makes a lot of sense to me as well.
rimo
CC BY-SA 4.0
2023-06-27T21:51:46.257Z
2023-06-27T21:51:46.257Z
gringer
<p>The <code>grep</code>, <code>cut</code>, <code>sed</code> pipeline could also be replaced with the following <a href="/questions/tagged/awk" class="post-tag" title="show questions tagged &#39;awk&#39;" aria-label="show questions tagged &#39;awk&#39;" rel="tag" aria-labelledby="tag-awk-tooltip-container">awk</a> command:</p>
<pre><code>awk '/^&gt;/ { print $1 &quot;_001&quot;; next }1' file.fa
</code></pre>
<p>The general syntax of an AWK command is <code>condition { action }</code> where <code>condition</code> specifies a condition or regular expression, and <code>action</code> specifies a block of code or set of commands to be run when the condition is met. Both <code>condition</code> and <code>action</code> are <em>optional</em>. Here, we use a regular expression to match lines beginning with a &quot;&gt;&quot; symbol, and print the first whitespace-separated field followed by &quot;_001&quot;. We then use <code>next</code> to skip the remaining actions and move to the next line, which avoids any further processing of the current line. The <code>1</code> on the end is actually another condition that always evaluates to true. Since there's no action associated with this condition, AWK performs the default action which is to print the entire line. This effectively prints all lines that do not begin with a &quot;&gt;&quot; symbol.</p>

CC BY-SA 4.0
2023-06-28T18:54:25.343Z
thank you this worked perfectly! and thank you for the explanation too
rimo
CC BY-SA 4.0
2023-06-28T00:42:22.460Z
2023-06-28T03:49:15.417Z
2023-06-28T03:49:15.417Z
Steve
<p>I have a FASTA header that looks like this:</p>
<pre><code>&gt;7c8250ef-c89f-4d42-9d48-12c8fe245fb2 runid=606f271fc97598006ba5a922136a2c304cef75a5 sampleid=Pool12-1 read=19008 ch=301 start_time=2021-07-03T08:48:18Z barcode=barcode01
</code></pre>
<p>And I am able to change it to the desired output here:</p>
<pre><code>&gt;7c8250ef-c89f-4d42-9d48-12c8fe245fb2_001
</code></pre>
<p>Using this command:</p>
<pre><code>grep '^&gt;' 001_old.fasta | cut -d ' ' -f 1,8 | sed '/^&gt;/s/$/_001/' &gt; 001_new.fasta
</code></pre>
<p>However it completely deletes the DNA sequence for that read and only outputs the names of the headers to the new fasta file:</p>
<pre><code>&gt;7c8250ef-c89f-4d42-9d48-12c8fe245fb2_001
&gt;815440d5-e033-446c-b7dd-5fad4105184a_001
&gt;d36a0fea-101d-4bd9-bee4-adec7fe4d43c_001
&gt;38ca9eb8-bbd2-4d0a-ab06-79d134ad46f1_001
...
</code></pre>
<p>I think it has something to do with the <code>cut</code> command I'm using. I tried just doing 1 but that gave the same output so I figured if I included the 8th position of text after a space it would work but again no dice. If someone could help me to include the DNA sequence under the FASTA header in this new file that would be great.</p>

CC BY-SA 4.0
2023-06-29T01:03:51.123Z
Do yourself a favor and use Bipython for parsing and modifying FASTA files.
Supertech
<fasta><grep><multi-fasta><command-line>
Keeping DNA sequence after changing FASTA header on command line
# Keeping DNA sequence after changing FASTA header on command line

(Asked by: username_0 on 2023-06-27)

I have a FASTA header that looks like this:
>7c8250ef-c89f-4d42-9d48-12c8fe245fb2 runid=606f271fc97598006ba5a922136a2c304cef75a5 sampleid=Pool12-1 read=19008 ch=301 start_time=2021-07-03T08:48:18Z barcode=barcode01

And I am able to change it to the desired output here:
>7c8250ef-c89f-4d42-9d48-12c8fe245fb2_001

Using this command:
grep '^>' 001_old.fasta | cut -d ' ' -f 1,8 | sed '/^>/s/$/_001/' > 001_new.fasta

However it completely deletes the DNA sequence for that read and only outputs the names of the headers to the new fasta file:
>7c8250ef-c89f-4d42-9d48-12c8fe245fb2_001
>815440d5-e033-446c-b7dd-5fad4105184a_001
>d36a0fea-101d-4bd9-bee4-adec7fe4d43c_001
>38ca9eb8-bbd2-4d0a-ab06-79d134ad46f1_001
...

I think it has something to do with the cut command I'm using. I tried just doing 1 but that gave the same output so I figured if I included the 8th position of text after a space it would work but again no dice. If someone could help me to include the DNA sequence under the FASTA header in this new file that would be great.

**Tags:** fasta, grep, multi-fasta, command-line
> **Comments:**
> **username_1**: Do yourself a favor and use Bipython for parsing and modifying FASTA files.  

---

## 2 answers

### Answer by username_3 on 2023-06-28. Score: 5 | Accepted answer

The grep, cut, sed pipeline could also be replaced with the following awk command:
awk '/^>/ { print $1 "_001"; next }1' file.fa

The general syntax of an AWK command is condition { action } where condition specifies a condition or regular expression, and action specifies a block of code or set of commands to be run when the condition is met. Both condition and action are optional. Here, we use a regular expression to match lines beginning with a ">" symbol, and print the first whitespace-separated field followed by "_001". We then use next to skip the remaining actions and move to the next line, which avoids any further processing of the current line. The 1 on the end is actually another condition that always evaluates to true. Since there's no action associated with this condition, AWK performs the default action which is to print the entire line. This effectively prints all lines that do not begin with a ">" symbol.

> **Comments:**
> **username_0**: thank you this worked perfectly! and thank you for the explanation too  

---

### Answer by username_2 on 2023-06-27. Score: 3

The problem is in the grep command you're using, which only sends the fasta file headers to the output. Here's my guess at what the commands in your pipeline do:

grep '^>' 001_old.fasta - extract lines starting with '>' from 001_old.fasta

cut -d ' ' -f 1,8 - assume file contains space-delimited records; extract only the first and eighth field from each line

sed '/^>/s/$/_001/' - in lines that begin with a '>', replace the end of the line with '_001'

> 001_new.fasta - write result out to 001_new.fasta


Of all of these, I think only the sed and output parts are important, but it needs to be slightly modified to work as intended:
sed '/^>/s/ .*$/_001/' 001_old.fasta > 001_new.fasta

Assuming I've got the syntax right, this should search for lines beginning with '>', and replace everything from the first space up to the end of the line with '_001'.
I'm more familiar with perl, so here's a perl one-liner that should do the same thing:
perl -pe '/^>/ && s/ .*$/_001/' 001_old.fasta > 001_new.fasta

> **Comments:**
> **username_0**: thank you so much! this makes a lot of sense to me as well.  

---

