count different characters from one column

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers count different characters from one column
# 1  
Old 03-21-2012
count different characters from one column

Hi everyone, and thanks after all

I'm a biologist and i have to extract information from one text. The text is something like this

1023    A    A    56    0    cc...,,,,,,c..CC..,,
1024    T    T    86    0    ..,,,..aaAA..,,aAA,,a,,A,,a
1025    G    G    125    0    ..,,,,..,,.ttt,,.,.,.TTttT.,.

The thing that I need is to print de column 1, 2, 3, 4 and 6.But in the sixth column I want to extract de information of different characters that are there. I want to print the the number of c or C and g or G and t or T and a or A of each line.
The thing that I use is just

gawk '{print $1,$2,$3,$4,$6, "\a"length($6)}' text

I need this and also the count of each.

If anyone could help me..


Last edited by Corona688; 03-21-2012 at 03:36 PM.. Reason: Code tags for code, please.
# 2  
Old 03-21-2012
Post desired output for this sample data.
# 3  
Old 03-21-2012
$ cat data

1023 A A 56 0 ccggCcccCC
1024 T T 86 0 aaAAaAAaAa
1025 G G 125 0 tttTTttT

$ awk '{
        $5=$6;         # Set field 5 to the contents of field 6
        $6=length($5); # Set field 6 to the length of field 5
        NF=6; # Chop off everything after field 6

        # Count the different characters
        for(N=1; N<=length($5); N++) C[substr($5,N,1)]++;
        # Add each different character as a new column, and clear
        # all values in C
        for(X in C) { $(NF+1)=X"="C[X]; delete C[X] }
        print # Print every line
}' data

1023 A A 56 ccggCcccCC 10 C=3 c=5 g=2
1024 T T 86 aaAAaAAaAa 10 A=5 a=5
1025 G G 125 tttTTttT 8 T=3 t=5


This User Gave Thanks to Corona688 For This Post:
# 4  
Old 03-22-2012

But I don't know how I have to use it... I'm new on this.. I have to copy all the script on a txt-editor and save it as .gawk? and then how I call it?
If it is very simple, can you recommend me any web or book?

# 5  
Old 03-22-2012
execute the below command in your console ( removed all the comments )

awk '{$5=$6;$6=length($5);NF=6;for(N=1; N<=length($5); N++) C[substr($5,N,1)]++;for(X in C) { $(NF+1)=X"="C[X]; delete C[X] }print}' input.txt

input.txt is your input file
# 6  
Old 03-22-2012
awk 'BEGIN{split("[Aa] [gG] [tT] [cC]",S)}{for(i in S){n=gsub(S[i],x,$6); s=s (s?",":x) S[i]"="n}NF-=1; $5=s; s=x}1' infile

1023 A A 56 [gG]=2,[tT]=0,[cC]=8,[Aa]=0
1024 T T 86 [gG]=0,[tT]=0,[cC]=0,[Aa]=10
1025 G G 125 [gG]=0,[tT]=8,[cC]=0,[Aa]=0

Last edited by Scrutinizer; 03-22-2012 at 06:25 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count consecutive characters

Need to count consecutive characters in a string and give the output as below i/p=aaaabbcaa o/p=a4b2c1a2 (10 Replies)
Discussion started by: prasanna2166
10 Replies

2. Shell Programming and Scripting

Column 2 string count in Column 3

My I/p is Col1|Col2|Col3 2116209997932|POSIX INC|POSIX 2116209997933|POSIX INC|POSIX 2116210089479|POSIX INC|POSIX 2116210180502|POSIX INC|POSIX 2116210512279|POSIX INC|Aero 2116210516838|POSIX INC|POSIX 2116210534342|POSIX INC|postal 2116210534345|POSIX INC|postal ... (6 Replies)
Discussion started by: nikhil jain
6 Replies

3. Shell Programming and Scripting

Read first column and count lines in second column using awk

Hello all, I would like to ask your help here: I've a huge file that has 2 columns. A part of it is: sorted.txt: kss23 rml.67lkj kss23 zhh.6gf kss23 nhd.09.fdd kss23 hp.767.88.89 fl67 nmdsfs.56.df.67 fl67 kk.fgf.98.56.n fl67 bgdgdfg.hjj.879.d fl66 kl..hfh.76.ghg fl66... (5 Replies)
Discussion started by: Padavan
5 Replies

4. Shell Programming and Scripting

Count characters after a line

Hi All! I would like to solve a problem but I have no clue of how do it!I will be grateful if someone could help me! I have a file like this: > genes | transcript ...sequence.... >ENSMUSG00000006638|ENSMUST00000006814 GGGAAATGGAATACCCCTACACAACCAAGATGCTGAGTTCCTCCCTGAGCCCGCAAAACG... (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

5. Shell Programming and Scripting

Remove the first character from the fourth column only if the column has four characters

I have a file as follows ATOM 5181 N AMET K 406 12.440 6.552 25.691 0.50 7.37 N ATOM 5182 CA AMET K 406 13.685 5.798 25.578 0.50 5.87 C ATOM 5183 C AMET K 406 14.045 5.179 26.909 0.50 5.07 C ATOM 5184 O MET K... (14 Replies)
Discussion started by: hasanabdulla
14 Replies

6. Shell Programming and Scripting

Count specific characters at specific column positions

Hi all, I need help. I have an input text file (input.txt) like this: 21 GTGCAACACCGTCTTGAGAGG 50 21 GACCGAGACAGAATGAAAATC 73 21 CGGGTCTGTAGTAGCAAACGC 108 21 CGAAAAATGAACCCCTTTATC 220 21 CGTGATCCTGTTGAAGGGTCG 259 Now I need to count A/T/G/C numbers at each character location in column... (2 Replies)
Discussion started by: thienxho
2 Replies

7. Shell Programming and Scripting

Count number of characters in particular column

Hi i have data like abchd 124 ldskc aattggcc each separated by tab space i want to count number of characters in 4th column and print it in new column with tabspace for every line can anyone help me how to do it. Thanks. (3 Replies)
Discussion started by: bhargavpbk88
3 Replies

8. UNIX for Dummies Questions & Answers

Count the characters in a string

Hi all, I like to know how to get the count of each character in a given word. Using the commands i can easily get the output. How do it without using the commands ( in shell programming or any programming) if you give outline of the program ( pseudo code ) i used the following commands ... (3 Replies)
Discussion started by: itkamaraj
3 Replies

9. Shell Programming and Scripting

awk count characters, sum, and divide by another column

Hi All, I am another biologist attempting to parse a large txt file containing several million lines like: tucosnp 56762 T Y 228 228 60 23 .CcCcc,,..c.c,cc,,.C... What I need to do is get the frequency of periods (.) plus commas (,) in column 9, and populate this number into another... (1 Reply)
Discussion started by: peromhc
1 Replies

10. Shell Programming and Scripting

count characters in specific records

I have a text file which represents a http flow like this: HTTP/1.1 200 OK Date: Fri, 23 Jan 2009 17:16:24 GMT Server: Apache Last-Modified: Fri, 23 Jan 2009 17:08:03 GMT Accept-Ranges: bytes Cache-Control: max-age=540 Expires: Fri, 23 Jan 2009 17:21:31 GMT Vary: Accept-Encoding ... (1 Reply)
Discussion started by: littleboyblu
1 Replies
Login or Register to Ask a Question

Featured Tech Videos