<p>You don't need a loop. You can do the whole thing with a simple awk one-liner:</p>

<pre><code>awk -vRS='&gt;' 'FNR&gt;1{ printf "&gt;%s",$0 &gt; "gene"FNR-1".fasta"}' org*fasta
</code></pre>

<p>I ran this on your files and got:</p>

<pre><code><span class="math-container">$ for file in gene*; do echo "=== File: $</span>file ==="; cat $file;  done
=== File: gene1.fasta ===
&gt;locus1 
ATGCGTAGAG
&gt;ysr
ATGTAGCGA
&gt;siv
TAGTAGTAT
=== File: gene2.fasta ===
&gt;LG12 
TATGCATGAT
&gt;inc
TGATCTATG
&gt;tdp
ATGAGTCGA
=== File: gene3.fasta ===
&gt;bpl
TAGATGGAT
&gt;pym
ATGCATAGA
&gt;tem
ATGCTHATG
</code></pre>

<h3>Explanation</h3>

<ul>
<li><code>-vRS='&gt;'</code> : set the record separator to <code>&gt;</code>. This will make <code>awk</code> consider every fasta sequence as a single "line". </li>
<li><code>FNR&gt;1{foo}</code> : <code>FNR</code> is the line number of the current file. Because <code>&gt;</code> is the first character of the entry, the first "line" will always be empty, so it needs t obe skipped.</li>
<li><code>printf "&gt;%s",$0 &gt; "gene"FNR-1".fasta"</code> : this does the work. It prints a <code>&gt;</code> followed by the current "line" (<code>$0</code>, this is the entire sequence + the header) and redirects it into a file called <code>gene</code>, then the current line number -1 (-1 is needed because of the issue mentioned above) and a <code>.fasta</code> extension.</li>
</ul>

<p>=====</p>

<p>Note that <code>org*fasta</code> will be expanded with the default collation order. This means that <code>org10.fasta</code> will be processed before <code>org1.fasta</code>:</p>

<pre><code>$ printf "org1.fasta\norg10.fasta\n" | sort 
org10.fasta
org1.fasta
</code></pre>

<p>To avoid this and have the files in numerical order, you can use brace expansion instead of the <code>*</code>. So, if you know your files are all <code>orgN.fasta</code> where <code>N</code> can take values from 1 to 20, do this:</p>

<pre><code>awk -vRS='&gt;' 'FNR&gt;1{ printf "&gt;%s",$0 &gt; "gene"FNR-1".fasta"}' org{1..20}.fasta
</code></pre>

CC BY-SA 4.0
2019-11-11T13:05:50.037Z
@K.Dineshkumar you're welcome! If one of the answers here solved your issue, please take a moment to [accept it](//$$SITEURL/help/accepted-answer) by clicking on [the checkmark on the left](https://meta.stackexchange.com/a/5235/203101). That will mark the question as answered and is the way that thanks are conveyed on the Stack Exchange sites.
terdon
CC BY-SA 4.0
2019-11-11T13:43:17.317Z
Thank you so much @terdon. However, I am facing a problem, this script perfectly fine for up to 9 number of fasta files. If it exceed 9, it takes 10th fasta file and printing first instead of 1st fasta file sequence. Same way, it prints 20th fasta file second instead of 2nd fasta file. I do not know how to solve this.
Kumar
CC BY-SA 4.0
2019-11-11T14:14:05.450Z
@K.Dineshkumar ah yes, that is because of the [order of collation](https://en.wikipedia.org/wiki/Collation). When you do `echo org*fasta`, it will sort alphabetically, so `org10.fasta` comes before `org1.fasta` (try with `printf "org1.fasta\norg10.fasta\n" | sort`). Try the updated answer.
terdon
CC BY-SA 4.0
2019-11-11T14:24:30.183Z
Many thanks @terdon for your detailed explanation. With your explanation, I learned how to play around the script, in order to serve our purpose.
Kumar
CC BY-SA 4.0
2019-11-11T11:16:34.000Z
2019-11-11T14:13:53.920Z
2019-11-11T14:13:53.920Z
terdon
<p>The title of your question indicates a <code>shell</code> solution, however you have added <code>R</code> and <code>Python</code> as tags. Here is an <code>R</code> solution making use of <code>Biostrings</code> pacakge (using two input files for simplicity):</p>

<pre><code>library(Biostrings)

# create Biostring objects from each multifasta file
org1 &lt;- readDNAStringSet("org1.fasta")
org2 &lt;- readDNAStringSet("org2.fasta")

# loop over the Biostring objects
for(i in 1:length(org1)){

  output_file &lt;- paste0("gene_", i, ".fasta")

  # create a Biostring object for each index
  genes_at_index_i &lt;- c(org1[i], org2[i])
  assign(gene_name, genes_at_index_i)

  # save Biostring object as a multifasta file
  writeXStringSet(genes_at_index_i, output_file)
}
</code></pre>

<p>Output, gene 1:</p>

<pre><code>&gt;locus1 
ATGCGTAGAG
&gt;ysr
ATGTAGCGA
</code></pre>

<p>Output, gene 2:</p>

<pre><code>&gt;LG12 
TATGCATGAT
&gt;inc
TGATCTATG
</code></pre>

<p>Warning: The code above won't work if you don't have the same number of fasta entries in each multifasta file.</p>

CC BY-SA 4.0
2019-11-11T13:25:24.177Z
Thank you @haci for your help. However, I could not use this approach for 100 fasta files or above. Creating variable for each fasta file is tedious job.
Kumar
CC BY-SA 4.0
2019-11-11T13:30:39.650Z
You can do so in a loop, i.e. creating a list of `Biostring` objects with `for` or `lapply` and then looping over that list.
haci
CC BY-SA 4.0
2019-11-11T13:43:48.423Z
Thank you @haci, I would try to do the same.
Kumar
CC BY-SA 4.0
2019-11-11T11:45:19.230Z
2019-11-11T11:45:19.230Z
haci
<p>I need to extract sequences one after another consecutively from a large fasta files (multiple fasta files) and each extracted files to be saved in new fasta file (I mean the first sequence extracted from multiple fasta file to be saved as gene1 and second sequence extracted from multiple fasta file to be saved in another file and so on). For that, I tried a script like this, </p>

<pre><code>for file in *.fasta do cat "<span class="math-container">$file" \
  | awk '/^&gt;/ {printf("%s%s\t",(N&gt;0?"\n":""),$0);N++;next;} \
          {printf("%s",$0);} \
     END {printf("\n");}' \
  | awk '{if ( NR==1 ) print $0;}' \
  | awk '{print $1"\n"$2}' | uniq &gt; "new_$</span>{file}" 
</code></pre>

<p>But, its not serving my purpose. Firstly, I could not loop my sequence extraction procedure from multiple fasta file and save as gene1 and gene2 and so on.</p>

<pre><code>For example,
</code></pre>

<p>I have fasta files as follows,</p>

<pre><code>org1.fasta
&gt;locus1 
ATGCGTAGAG
&gt;LG12 
TATGCATGAT
&gt;bpl
TAGATGGAT

org2.fasta
&gt;ysr
ATGTAGCGA
&gt;inc
TGATCTATG
&gt;pym
ATGCATAGA

org3.fasta
&gt;siv
TAGTAGTAT
&gt;tdp
ATGAGTCGA
&gt;tem
ATGCTHATG
</code></pre>

<p>I need to extract first positioned sequences from all the <code>org1.fasta org2.fasta and org3.fasta</code> files and save in a new file <code>gene1.fasta</code> and second positioned sequences as <code>gene2.fasta</code>and third positioned sequences to be saved as <code>gene3.fasta</code>. </p>

<pre><code>Expected output,

gene1.fasta
&gt;locus1 
ATGCGTAGAG
&gt;ysr
ATGTAGCGA
&gt;siv
TAGTAGTAT

gene2.fasta
&gt;LG12 
TATGCATGAT
&gt;inc
TGATCTATG
&gt;tdp
ATGAGTCGA

gene3.fasta
&gt;bpl
TAGATGGAT
&gt;pym
ATGCATAGA
&gt;tem
ATGCTHATG
</code></pre>

<p>Please help me to do the same. 
Thanks in advance.</p>

<r><python><bash><perl><shell>
How to loop multiple function in shell script?
# How to loop multiple function in shell script?

(Asked by: username_0 on 2019-11-11)

I need to extract sequences one after another consecutively from a large fasta files (multiple fasta files) and each extracted files to be saved in new fasta file (I mean the first sequence extracted from multiple fasta file to be saved as gene1 and second sequence extracted from multiple fasta file to be saved in another file and so on). For that, I tried a script like this, 
for file in *.fasta do cat "$file" \
  | awk '/^>/ {printf("%s%s\t",(N>0?"\n":""),$0);N++;next;} \
          {printf("%s",$0);} \
     END {printf("\n");}' \
  | awk '{if ( NR==1 ) print $0;}' \
  | awk '{print $1"\n"$2}' | uniq > "new_${file}" 

But, its not serving my purpose. Firstly, I could not loop my sequence extraction procedure from multiple fasta file and save as gene1 and gene2 and so on.
For example,

I have fasta files as follows,
org1.fasta
>locus1 
ATGCGTAGAG
>LG12 
TATGCATGAT
>bpl
TAGATGGAT

org2.fasta
>ysr
ATGTAGCGA
>inc
TGATCTATG
>pym
ATGCATAGA

org3.fasta
>siv
TAGTAGTAT
>tdp
ATGAGTCGA
>tem
ATGCTHATG

I need to extract first positioned sequences from all the org1.fasta org2.fasta and org3.fasta files and save in a new file gene1.fasta and second positioned sequences as gene2.fastaand third positioned sequences to be saved as gene3.fasta. 
Expected output,

gene1.fasta
>locus1 
ATGCGTAGAG
>ysr
ATGTAGCGA
>siv
TAGTAGTAT

gene2.fasta
>LG12 
TATGCATGAT
>inc
TGATCTATG
>tdp
ATGAGTCGA

gene3.fasta
>bpl
TAGATGGAT
>pym
ATGCATAGA
>tem
ATGCTHATG

Please help me to do the same. 
Thanks in advance.

**Tags:** r, python, bash, perl, shell

---

## 2 answers

### Answer by username_1 on 2019-11-11. Score: 3 | Accepted answer

You don't need a loop. You can do the whole thing with a simple awk one-liner:
awk -vRS='>' 'FNR>1{ printf ">%s",$0 > "gene"FNR-1".fasta"}' org*fasta

I ran this on your files and got:
$ for file in gene*; do echo "=== File: $file ==="; cat $file;  done
=== File: gene1.fasta ===
>locus1 
ATGCGTAGAG
>ysr
ATGTAGCGA
>siv
TAGTAGTAT
=== File: gene2.fasta ===
>LG12 
TATGCATGAT
>inc
TGATCTATG
>tdp
ATGAGTCGA
=== File: gene3.fasta ===
>bpl
TAGATGGAT
>pym
ATGCATAGA
>tem
ATGCTHATG

Explanation

-vRS='>' : set the record separator to >. This will make awk consider every fasta sequence as a single "line". 
FNR>1{foo} : FNR is the line number of the current file. Because > is the first character of the entry, the first "line" will always be empty, so it needs t obe skipped.
printf ">%s",$0 > "gene"FNR-1".fasta" : this does the work. It prints a > followed by the current "line" ($0, this is the entire sequence + the header) and redirects it into a file called gene, then the current line number -1 (-1 is needed because of the issue mentioned above) and a .fasta extension.

=====
Note that org*fasta will be expanded with the default collation order. This means that org10.fasta will be processed before org1.fasta:
$ printf "org1.fasta\norg10.fasta\n" | sort 
org10.fasta
org1.fasta

To avoid this and have the files in numerical order, you can use brace expansion instead of the *. So, if you know your files are all orgN.fasta where N can take values from 1 to 20, do this:
awk -vRS='>' 'FNR>1{ printf ">%s",$0 > "gene"FNR-1".fasta"}' org{1..20}.fasta

> **Comments:**
> **username_1**: @K.Dineshkumar you're welcome! If one of the answers here solved your issue, please take a moment to [accept it](//$$SITEURL/help/accepted-answer) by clicking on [the checkmark on the left](https://meta.stackexchange.com/a/5235/203101). That will mark the question as answered and is the way that thanks are conveyed on the Stack Exchange sites.  
> **username_0**: Thank you so much @terdon. However, I am facing a problem, this script perfectly fine for up to 9 number of fasta files. If it exceed 9, it takes 10th fasta file and printing first instead of 1st fasta file sequence. Same way, it prints 20th fasta file second instead of 2nd fasta file. I do not know how to solve this.  
> **username_1**: @K.Dineshkumar ah yes, that is because of the [order of collation](https://en.wikipedia.org/wiki/Collation). When you do `echo org*fasta`, it will sort alphabetically, so `org10.fasta` comes before `org1.fasta` (try with `printf "org1.fasta\norg10.fasta\n" | sort`). Try the updated answer.  
> **username_0**: Many thanks @terdon for your detailed explanation. With your explanation, I learned how to play around the script, in order to serve our purpose.  

---

### Answer by username_2 on 2019-11-11. Score: 1

The title of your question indicates a shell solution, however you have added R and Python as tags. Here is an R solution making use of Biostrings pacakge (using two input files for simplicity):
library(Biostrings)

# create Biostring objects from each multifasta file
org1 <- readDNAStringSet("org1.fasta")
org2 <- readDNAStringSet("org2.fasta")

# loop over the Biostring objects
for(i in 1:length(org1)){

  output_file <- paste0("gene_", i, ".fasta")

  # create a Biostring object for each index
  genes_at_index_i <- c(org1[i], org2[i])
  assign(gene_name, genes_at_index_i)

  # save Biostring object as a multifasta file
  writeXStringSet(genes_at_index_i, output_file)
}

Output, gene 1:
>locus1 
ATGCGTAGAG
>ysr
ATGTAGCGA

Output, gene 2:
>LG12 
TATGCATGAT
>inc
TGATCTATG

Warning: The code above won't work if you don't have the same number of fasta entries in each multifasta file.

> **Comments:**
> **username_0**: Thank you @haci for your help. However, I could not use this approach for 100 fasta files or above. Creating variable for each fasta file is tedious job.  
> **username_2**: You can do so in a loop, i.e. creating a list of `Biostring` objects with `for` or `lapply` and then looping over that list.  
> **username_0**: Thank you @haci, I would try to do the same.  

---

