Reading character by character - BASH


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reading character by character - BASH
# 1  
Old 10-28-2011
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.
# 2  
Old 10-28-2011
read can be used to read 1 (or N) bytes at a time, e.g.

Code:
# 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

# 3  
Old 10-28-2011
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:

Code:
#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]);
}


Last edited by Corona688; 10-28-2011 at 01:44 PM..
# 4  
Old 10-28-2011
Thanks to you, I have been able to do exactly what I wanted to do !

I'm so happy Smilie
Have a nice day !
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to understand special character for line reading in bash shell?

I am still learning shell scripting. Recently I see a function for read configuration. But some of special character make me confused. I checked online to find answer. It was not successful. I post the code here to consult with expert or guru to get better understanding on these special characters... (3 Replies)
Discussion started by: duke0001
3 Replies

2. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

3. Shell Programming and Scripting

Preserve spaces while reading character character

Hi All, I am trying to read a file character by character, #!/bin/bash while read -n1 char; do echo -e "$char\c" done < /home/shak/testprogram/words Newyork is a very good city. Newyorkisaverygoodcityforliving I need to preserve the spaces as thats an... (3 Replies)
Discussion started by: Kingcobra
3 Replies

4. Shell Programming and Scripting

Problem with character by character reading

Hi friend, i have the following problem: when i am writting the below command on the command prompt , its working. while read -n 1 ch; do echo "$ch" ; echo "$ch" ; done<file_name.out. but when i am executing it after saving it in a ksh file, its not working. Please helppppppppp .. thankss... (18 Replies)
Discussion started by: neelmani
18 Replies

5. Shell Programming and Scripting

Reading a single character from each line of the file

Hi, I should read one character at a fixed position from each line of the file. So how ??? should be substituted in the code below: while read line ; do single_char=`???` echo "$single_char" done < $input_file (8 Replies)
Discussion started by: arsii
8 Replies

6. HP-UX

reading password and echoing '*' character on console

hi all, I am using HP-UX system. I want echoing * characters while reading password through keyboard instead of blank space. can u help me for that code? Thanks (1 Reply)
Discussion started by: hari_uctech
1 Replies

7. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

8. Programming

Strange character added when reading to buffer with length of 12

Hi all, I got trouble with reading and writing ( to standard input/output, file, socket whatever...). I will briefly describe it by giving this example. I want to read a long string from keyboard but i don't know how long it is b4. So i need to know the number of character i will read first.... (6 Replies)
Discussion started by: tazan_007
6 Replies

9. Shell Programming and Scripting

Reading password and echo * character

Hi, First of all i am using solaris 10. I want to write a script that ask user to enter password and read the character input from keyboard. The ask to re-enter the password and then if they are match it will accept. But my problem is I want to echo a '*' character instead of the character I... (4 Replies)
Discussion started by: alanpachuau
4 Replies

10. Programming

reading a single character in C

Can anyone help me????? My problem is that i want to read only one charcter from keyboard. Each time my program waits to press enter or ^d. I don't want that. As soon as i press a charcter it should proceed to next statement in program without pressing enter or ^d. please help... (3 Replies)
Discussion started by: alodha
3 Replies
Login or Register to Ask a Question