Counting 2 characters with into 2 categories from a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting 2 characters with into 2 categories from a text file
# 1  
Old 03-21-2012
Counting 2 characters with into 2 categories from a text file

I have a tab delimited file of the following format

Code:
2 L a
2 G b
2 L c
2 G a
3 G a
3 G b
3 L c
4 L a
4 G a
4 G b
4 L c
4 G a
..
...

I want to count the number of G's and L's with in the first column and the third column/categories such that I would get an output file:
Code:
        a           b          c
    #L #G    #L #G     #L #G  
2   1     1     1   0     1    0
3   0     1     0   1     1    0
4   1     2     0   1     1    0

Please let me know the best way to count this using awk or sed. I don't need the headers but a tab delimited file with an order would be great.
Thanks
# 2  
Old 03-21-2012
maybe this thread can help a bit: https://www.unix.com/shell-programmin...lues-file.html

you can create an index-name out of col3+col1.

pseudocode:
Code:
if "L" -> yourArr[col3&col1][0]++
if "G" -> yourArr[col3&col1][1]++

# 3  
Old 03-21-2012
Still having problems. Tried the following code but it didn't give the result wanted:
Code:
awk 'NR==FNR{a[$1]=$2" "$3" "a[$1];next} END{for(b in a)print b,a[b]}' infile

Help is really appreciated
# 4  
Old 03-21-2012
you first will have to set a field seperator ( parameter or variable FS ) in awk. default is "\n".

where is check for value in 2nd column and incrementation of value in array in the command you posted?
# 5  
Old 03-21-2012
Tried..it but still synthax error. I am not an expert in awk
# 6  
Old 03-21-2012
Quote:
Originally Posted by Lucky Ali
Help is really appreciated
Ok Smilie

you want to seperate into columns (delimiter is space)?

awk syntax for defining it are awk -F" " ... or awk '{ FS=" "; }'

now fields in awk are stored in $1,$2, ...

to create index name you can use following awk syntax: indx=$3" "$1;

syntax to store values into an array: { yourArr[0][1]="text"; }

in this example yourArr is a multidimensional array. row 0, col 1 contains now string "text".

condition statement in awk: { if($0=="input text") { printf "input string equals \"input text\""; }}

now you have to put all this together.. give it a try?
# 7  
Old 03-21-2012
Code:
# cat infile
2 L a
2 G b
2 L c
2 G a
3 G a
3 G b
3 L c
4 L a
4 G a
4 G b
4 L c
4 G a

Code:
# awk '{a[$1FS$2]=a[$1FS$2]FS$3}END{for(i in a){split(a[i],b);d=1;x=1;k=0;w="";for(x=1;x<=length(b);x++){
k++;c=0;;for(j=1;j<=length(b);j++){if(b[x]==b[j])c++;};cc[x]=c;;e[k]=b[x];;};
for(k=1;k<=length(b);k++){if(!match(w,e[k]))printf "%5s%s%d%s%5s",e[k],"---> ",cc[k]," count in the ",i;w=w" "e[k]}
printf "%s","\n";}}' infile
    a---> 1 count in the   2 L    c---> 1 count in the   2 L
    c---> 1 count in the   3 L
    a---> 1 count in the   4 L    c---> 1 count in the   4 L
    b---> 1 count in the   2 G    a---> 1 count in the   2 G
    a---> 1 count in the   3 G    b---> 1 count in the   3 G
    a---> 2 count in the   4 G    b---> 1 count in the   4 G

regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting characters at each position

Hi All, here's a question from newbie I have a data like this, which set of small DNA sequences separated by new line GAATCCGGAAACAGCAACTTCAAANCA GTNATTCGGGCCAAACTGTCGAA TTNGGCAACTGTTAGAGCTCATGCGACA CCTGCTAAACGAGTTCGAGTTGAANGA TTNCGGAAGTGGTCGCTGGCACGG ACNTGCATGTACGGAGTGACGAAACCI... (6 Replies)
Discussion started by: amits22
6 Replies

2. Shell Programming and Scripting

Counting the number of characters

Hi all, Can someone help me in getting the following o/p I/p:... (7 Replies)
Discussion started by: Sri3001
7 Replies

3. Shell Programming and Scripting

Counting characters within a file

Ok say I wanted to count every Y in a data file. Then set Y as my delimiter so that I can separate my file by taking all the contents that occur BEFORE the first Y and store them in a variable so that I may use this content later on in my program. Then I could do the same thing with the next Y's... (5 Replies)
Discussion started by: puttster
5 Replies

4. Shell Programming and Scripting

taking characters and counting them

Nevermind, I figured out a way using the sed command. But I forget the basic way of counting characters within a variable :( (4 Replies)
Discussion started by: puttster
4 Replies

5. Shell Programming and Scripting

Counting characters with sed

Input: ghw//yw/hw///??u How can i count the slashes("/") using sed? (13 Replies)
Discussion started by: cola
13 Replies

6. Shell Programming and Scripting

counting characters

Hi All, I need some help in counting the number of letters in a big file with separations. Following is the file I have >AB_1 MLKKPIIIGVTGGSGGGKTSVSRAILDSFPNARIAMIQHDSYYKDQSHMSFEERVKTNYDHPLAFDTDFM IQQLKELLAGRPVDIPIYDYKKHTRSNTTFRQDPQDVIIVEGILVLEDERLRDLMDIKLFVDTDDDIRII... (6 Replies)
Discussion started by: Lucky Ali
6 Replies

7. UNIX for Dummies Questions & Answers

counting the occurence of particular characters

I want to list the occurence of particular characters in a line. my file looks like this a,b,c,d e,f,g h,y:e,g,y s f;g,s,w and I want to count how many commas are in each line so the file in the end looks like this: a,b,c,d 3 e,f,g 2 h,y:e,g,y s 3 f;g,s,w ... (2 Replies)
Discussion started by: Audra
2 Replies

8. Shell Programming and Scripting

Deleting the blank line in a file and counting the characters....

Hi, I am trying to do two things in my script. I will really appreciate any help in this regards. Is there a way to delete a last line from a pipe delimited flat file if the last line is blank. If the line is not blank then do nothing..... Is there a way to count a word that are starting... (4 Replies)
Discussion started by: rkumar28
4 Replies

9. Shell Programming and Scripting

Counting characters between comma's

I have a comma delimited file that roughly has 300 fields. Not all fields are populated. This file is fed into another system, what I need to do is count the amount of characters in each field and give me an output similiar to this: 1 - 6,2 - 25 The first number is the field and the second... (2 Replies)
Discussion started by: dbrundrett
2 Replies

10. Shell Programming and Scripting

counting characters

Dears, I would like to count the number of "(" and ")" that occur in a file. (syntax checking script). I tried to use "grep -c" and this works fine as long as there is only one character (for which I do a search) on a line. Has anyone an idea how I can count the number of specific characters... (6 Replies)
Discussion started by: plelie2
6 Replies
Login or Register to Ask a Question