Dear All,

I have a big embl file that has over 1000 IDs, and I want to split it based on IDs. Each ID should be a separate file containing all information of related ID, and should have the ID name.

How to do that?

One option:

    $ awk -v RS="ID " -v FS="\n" {'n = split($1, a, ";"); sub(/ /, "_", a[1]); a[1] = a[1]".embl"; outFn = a[1]; record = "ID "$0; printf("%s", record) > outFn;'} ./in.embl

Given test input:

    $ more test.embl
    ID AA03518 standard; DNA; FUN; 237 BP.
    XX
    AC U03518;
    XX
    DE Aspergillus awamori internal transcribed spacer 1 (ITS1) and 18S
    DE rRNA and 5.8S rRNA genes, partial sequence.
    XX
    SQ Sequence 237 BP; 41 A; 77 C; 67 G; 52 T; 0 other;
       aacctgcgga aggatcatta ccgagtgcgg gtcctttggg cccaacctcc catccgtgtc 60
       tattgtaccc tgttgcttcg gcgggcccgc cgcttgtcgg ccgccggggg ggcgcctctg 120 
       ccccccgggc ccgtgcccgc cggagacccc aacacgaaca ctgtctgaaa gcgtgcagtc 180
       tgagttgatt gaatgcaatc agttaaaact ttcaacaatg gatctcttgg ttccggc 237
    //
    ID AA03519 standard; DNA; FUN; 237 BP.
    XX
    AC U03518;
    XX
    DE Aspergillus awamori internal transcribed spacer 1 (ITS1) and 18S
    DE rRNA and 5.8S rRNA genes, partial sequence.
    XX
    SQ Sequence 237 BP; 41 A; 77 C; 67 G; 52 T; 0 other;
       aacctgcgga aggatcatta ccgagtgcgg gtcctttggg cccaacctcc catccgtgtc 60
       tattgtaccc tgttgcttcg gcgggcccgc cgcttgtcgg ccgccggggg ggcgcctctg 120 
       ccccccgggc ccgtgcccgc cggagacccc aacacgaaca ctgtctgaaa gcgtgcagtc 180
       tgagttgatt gaatgcaatc agttaaaact ttcaacaatg gatctcttgg ttccggc 237
    //
    ID AA03520 standard; DNA; FUN; 237 BP.
    XX
    AC U03518;
    XX
    DE Aspergillus awamori internal transcribed spacer 1 (ITS1) and 18S
    DE rRNA and 5.8S rRNA genes, partial sequence.
    XX
    SQ Sequence 237 BP; 41 A; 77 C; 67 G; 52 T; 0 other;
       aacctgcgga aggatcatta ccgagtgcgg gtcctttggg cccaacctcc catccgtgtc 60
       tattgtaccc tgttgcttcg gcgggcccgc cgcttgtcgg ccgccggggg ggcgcctctg 120 
       ccccccgggc ccgtgcccgc cggagacccc aacacgaaca ctgtctgaaa gcgtgcagtc 180
       tgagttgatt gaatgcaatc agttaaaact ttcaacaatg gatctcttgg ttccggc 237
    //
    ID AA03521 standard; DNA; FUN; 237 BP.
    XX
    AC U03518;
    XX
    DE Aspergillus awamori internal transcribed spacer 1 (ITS1) and 18S
    DE rRNA and 5.8S rRNA genes, partial sequence.
    XX
    SQ Sequence 237 BP; 41 A; 77 C; 67 G; 52 T; 0 other;
       aacctgcgga aggatcatta ccgagtgcgg gtcctttggg cccaacctcc catccgtgtc 60
       tattgtaccc tgttgcttcg gcgggcccgc cgcttgtcgg ccgccggggg ggcgcctctg 120 
       ccccccgggc ccgtgcccgc cggagacccc aacacgaaca ctgtctgaaa gcgtgcagtc 180
       tgagttgatt gaatgcaatc agttaaaact ttcaacaatg gatctcttgg ttccggc 237
    //

Run the script:

    $ awk -v RS="ID " -v FS="\n" {'n = split($1, a, ";"); sub(/ /, "_", a[1]); a[1] = a[1]".embl"; outFn = a[1]; record = "ID "$0; printf("%s", record) > outFn;'} ./test.embl

This creates four files, one for each record, named by the record's ID:

    $ ls -al AA*.embl
    -rw-r--r--  1 areynolds  wheel   519 Oct 26 15:01 AA03518_standard.embl
    -rw-r--r--  1 areynolds  wheel   519 Oct 26 15:01 AA03519_standard.embl
    -rw-r--r--  1 areynolds  wheel   519 Oct 26 15:01 AA03520_standard.embl
    -rw-r--r--  1 areynolds  wheel   519 Oct 26 15:01 AA03521_standard.embl

For example:

    $ more AA03520_standard.embl 
    ID AA03520 standard; DNA; FUN; 237 BP.
    XX
    AC U03518;
    XX
    DE Aspergillus awamori internal transcribed spacer 1 (ITS1) and 18S
    DE rRNA and 5.8S rRNA genes, partial sequence.
    XX
    SQ Sequence 237 BP; 41 A; 77 C; 67 G; 52 T; 0 other;
       aacctgcgga aggatcatta ccgagtgcgg gtcctttggg cccaacctcc catccgtgtc 60
       tattgtaccc tgttgcttcg gcgggcccgc cgcttgtcgg ccgccggggg ggcgcctctg 120 
       ccccccgggc ccgtgcccgc cggagacccc aacacgaaca ctgtctgaaa gcgtgcagtc 180
       tgagttgatt gaatgcaatc agttaaaact ttcaacaatg gatctcttgg ttccggc 237
    //

If IDs are not uniquely namable, then this could cause previous results to get overwritten. It may be safer to use `>>` instead of `>` within the `awk` statement.