Reading character by character - BASH

Hello every one and thanks in advance for the time you will take to think about my problem.

I would like to know if it's possible (in BASH) to read a text file character after character.

Exactly this is what I would like to do :

Txt file : ATGCAGTTCATTGCCAAA...... (~2.5 millions characters)

First letter is A ==> increment $A by one
Second letter is T ==> increment $T by one
....

Then, do the same for 2 letters' groups... 3 letters' groups and so on.

Is it possible in BASH or should I try an other language ? (in this case, wich one ?)

Thank you,
Sluvah.

read can be used to read 1 (or N) bytes at a time, e.g.

# cat xx.txt
ATGCAGTTCATTGCCAAA

# cat xx.sh
#!/bin/bash

while read -n1 achar
do
   echo $achar
done < xx.txt

# ./xx.sh
A
T
G
C
A
G
T
T
C
A
T
T
G
C
C
A
A
A

That is not a very efficient way to do this, though. No scripting language is really going to cut it.

If you really have to parse character-by-character, ideally you could use C:

#include <stdio.h>
#include <string.h>

int main(void)
{
        int c, count[256];

        memset(count, 0, sizeof(count));

        while((c=getc()) >= 0)    count[c]++;

        for(c=0; c<256; c++)
        if(count[c])
                printf("%c\t%d\n", c, count[c]);
}

Thanks to you, I have been able to do exactly what I wanted to do !

I'm so happy :smiley:
Have a nice day !