<p>I couldn't come up with a one liner but have a two liner instead...</p>

<p>To get the start positions of the N repeat:</p>

<pre><code>grep N mySeq.fasta | grep -b -o N | awk -F: '{print $1}' |awk '$1!=p+1{print $1}{p=$1}'
12
38
56
</code></pre>

<p>To get the length of the repeats:</p>

<pre><code>grep N mySeq.fasta | awk -F '[^N]+' '{for (i=1; i&lt;=NF; i++) if ($i != "") print length($i)}' 
14
6
9
</code></pre>

<p>With thanks to stack overflow.</p>

CC BY-SA 3.0
2017-08-31T11:13:51.977Z
2017-08-31T11:20:13.063Z
2017-08-31T11:20:13.063Z
benn
<p>I have a sequence file and want to count consecutive character "N", with the tandem's position of occurrence and its length. Say I have a file named <code>mySequence.fasta</code> like this:</p>

<pre><code>&gt;sequence-1 
ATCGCTAGCATNNNNNNNNNNNNNNCTAGCATCATGCNNNNNNATACGCATCACANNNNNNNNNCgcatATCAC
</code></pre>

<p>and anticipated output should be like this:</p>

<pre><code>Position 12 N 15
Position 38 N 6
Position 56 N 9
</code></pre>

<p>Kindly help me to solve this by <code>awk</code> or <code>sed</code> providing my file name <code>mySequence.fasta</code></p>

CC BY-SA 3.0
2017-08-31T14:24:57.420Z
Why are you tied to awk and sed?  There are many tools in programming languages like python, perl and java that are designed for this.
Bioathlete
<fasta><text-processing>
Counting a specific consecutive character with its occurrence position and length
# Counting a specific consecutive character with its occurrence position and length

(Asked by: username_0 on 2017-08-31)

I have a sequence file and want to count consecutive character "N", with the tandem's position of occurrence and its length. Say I have a file named mySequence.fasta like this:
>sequence-1 
ATCGCTAGCATNNNNNNNNNNNNNNCTAGCATCATGCNNNNNNATACGCATCACANNNNNNNNNCgcatATCAC

and anticipated output should be like this:
Position 12 N 15
Position 38 N 6
Position 56 N 9

Kindly help me to solve this by awk or sed providing my file name mySequence.fasta

**Tags:** fasta, text-processing
> **Comments:**
> **username_1**: Why are you tied to awk and sed?  There are many tools in programming languages like python, perl and java that are designed for this.  

---

## 1 answers

### Answer by username_2 on 2017-08-31. Score: 2

I couldn't come up with a one liner but have a two liner instead...
To get the start positions of the N repeat:
grep N mySeq.fasta | grep -b -o N | awk -F: '{print $1}' |awk '$1!=p+1{print $1}{p=$1}'
12
38
56

To get the length of the repeats:
grep N mySeq.fasta | awk -F '[^N]+' '{for (i=1; i<=NF; i++) if ($i != "") print length($i)}' 
14
6
9

With thanks to stack overflow.


---

