<pre><code>grep -v "&gt;" multi.fasta | tr -d '\n' | wc -c
</code></pre>

<ul>
<li><code>grep -v "&gt;"</code> find all non header lines (i.e. without ">")</li>
<li><code>tr -d '\n'</code> delete all newlines</li>
<li><code>wc -c</code> count all characters</li>
</ul>

CC BY-SA 3.0
2017-11-23T12:01:27.103Z
2017-11-23T12:01:27.103Z
Kamil S Jaron
<p>Here’s a function that I keep around for calculating the individual lengths of fasta sequences</p>

<pre><code>falens(){
awk '/^&gt;/ {if (seqlen){print seqlen}; print ;seqlen=0;next; } { seqlen += length($0)}END{print seqlen}' $1
}
</code></pre>

<p>And one I use for summing a column of numbers</p>

<pre><code>sum(){
awk '{ sum += $1 } END { print sum}'
}
</code></pre>

<p>You can combine these by piping the output to do the sum for you:</p>

<pre><code>falens myseqs.fa | grep -v “&gt;” | awk '{ sum += $1 } END { print sum}'
</code></pre>

<p>(Can’t test this myself currently as I’m not at my laptop, but it’s based on 2 functions that work normally)</p>

CC BY-SA 3.0
2017-11-16T18:40:02.707Z
2017-11-16T18:40:02.707Z
Joe Healey
<p>I have a multifasta file containing several genes from a genomic island. I want to know the total length of this island. How can I do this?</p>

CC BY-SA 3.0
2017-11-15T14:47:17.937Z
What have you tried? How many sequences do you have? How are the position of each sequence stored in your multifasta or elsewhere?
llrs
CC BY-SA 3.0
2017-11-15T15:06:54.747Z
Most tools that calculate genome assembly summary stats will report total length. I use [assembly-stats](https://github.com/sanger-pathogens/assembly-stats)
heathobrien
<fasta>
How to find the sum of all sequences in a multifasta file
# How to find the sum of all sequences in a multifasta file

(Asked by: username_0 on 2017-11-15)

I have a multifasta file containing several genes from a genomic island. I want to know the total length of this island. How can I do this?

**Tags:** fasta
> **Comments:**
> **username_1**: What have you tried? How many sequences do you have? How are the position of each sequence stored in your multifasta or elsewhere?  
> **username_2**: Most tools that calculate genome assembly summary stats will report total length. I use [assembly-stats](https://github.com/sanger-pathogens/assembly-stats)  

---

## 2 answers

### Answer by username_3 on 2017-11-23. Score: 5

grep -v ">" multi.fasta | tr -d '\n' | wc -c


grep -v ">" find all non header lines (i.e. without ">")
tr -d '\n' delete all newlines
wc -c count all characters


---

### Answer by username_4 on 2017-11-16. Score: 2

Here’s a function that I keep around for calculating the individual lengths of fasta sequences
falens(){
awk '/^>/ {if (seqlen){print seqlen}; print ;seqlen=0;next; } { seqlen += length($0)}END{print seqlen}' $1
}

And one I use for summing a column of numbers
sum(){
awk '{ sum += $1 } END { print sum}'
}

You can combine these by piping the output to do the sum for you:
falens myseqs.fa | grep -v “>” | awk '{ sum += $1 } END { print sum}'

(Can’t test this myself currently as I’m not at my laptop, but it’s based on 2 functions that work normally)


---

