Counts based on occurances


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counts based on occurances
# 1  
Old 09-26-2011
Counts based on occurances

Hi,

I have a file with 2500 entries. There are many duplicates,triplicates symbols in my file in the first column and the second column has categories(high/medium/low) . I want to have count for the occurances of each category for each unique symbol

Code:
ABC   high
ABC   high
ABC   medium
ABC   low
ABC   low
XYZ   low
XYZ   low
XYZ   low
XYZ   high
XYZ   high

output:
Symbol high medium low
Code:
ABC  2  1  2
XYZ  2 0  3

Thanks,
# 2  
Old 09-26-2011
If the categories are as shown:

Code:
$ awk '{ S[$1]=1 ; V[$1 $2]++ }
 END {
         for(K in S)
                 printf("%s\t%d\t%d\t%d\n", K, V[K "low"], V[K "medium"], V[K "high"]);
         }' < data

ABC     2       1       2
XYZ     3       0       2
$

---------- Post updated at 10:29 AM ---------- Previous update was at 10:25 AM ----------

Or, if there's actually more categories than that:

Code:
$ cat anycat.awk
{       S[$1]=1;        C[$2]=1;        V[$1 $2]++;     }
END {
        printf("#");
        for(COL in C)   printf("\t%s", COL);
        printf("\n");

        for(K in S)
        {
                printf("%s", K);
                for(COL in C)
                {
                        printf("\t%d", V[K COL]);
                }

                printf("\n");
        }
}
$ awk -f anycat.awk < data
#       high    low     medium
ABC     2       2       1
XYZ     2       3       0
$

# 3  
Old 09-26-2011
Thank you..It worked Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

2 files replace multiple occurances based on a match

Hi All, I need some help trying to achieve the below but everything I've tried has failed, I have 2 files which i'm trying to carry out a match based on the first column from file 1, take that value find it in file 2 if found replace it with the second column from File 1 Lookup File: File 1... (3 Replies)
Discussion started by: mutley2202
3 Replies

2. Shell Programming and Scripting

occurances

Hi, I have 2 files File 1 ABC XYZ MNO WER FDS CFG File 2 ABC_123456_234567 ABC_123456_234567 ABC_123456_234567 ABC_123456_234567 ABC_123456_234567 (1 Reply)
Discussion started by: Diya123
1 Replies

3. Shell Programming and Scripting

Filtering lines for column elements based on corresponding counts in another column

Hi, I have a file like this ACC 2 2 21 aaa AC 443 3 22 aaa GCT 76 1 33 xxx TCG 34 2 33 aaa ACGT 33 1 22 ggg TTC 99 3 44 wee CCA 33 2 33 ggg AAC 1 3 55 ddd TTG 10 1 22 ddd TTGC 98 3 22 ddd GCT 23 1 21 sds GTC 23 4 32 sds ACGT 32 2 33 vvv CGT 11 2 33 eee CCC 87 2 44... (1 Reply)
Discussion started by: polsum
1 Replies

4. Shell Programming and Scripting

counts based on percentage

I have a file with multiple entries and I have calculated the percentages. Now I want to know how many of my entries are there between 1-10% 11-20% and so on.. chr1_14401_14450 0.211954217888936 chr1_14451_14500 1.90758796100042 chr1_14501_14550 4.02713013988978 chr1_14551_14600 ... (3 Replies)
Discussion started by: Diya123
3 Replies

5. UNIX for Dummies Questions & Answers

changing all occurances

how wouldi change all occurances of that to they regardless of the number of times it appears on a line? (2 Replies)
Discussion started by: trob
2 Replies

6. UNIX for Dummies Questions & Answers

replacing all occurances

How would i replace all occurreneces of Tim(ignoring the case) with Timithoy in the file "file1" and then save it to "newfile" (2 Replies)
Discussion started by: JamieMurry
2 Replies

7. Shell Programming and Scripting

removing occurances starting with [

I have this line in a file gtgti1/gtgticcrunit1/gtgti_ccrunit_10n/port_arbiter1/port7 = gtgti1/gtgticcrunit1/gtgti_ccrunit_10n/port_arbiter1/port7; gtgti1/gtgticcrunit1/gtgti_ccrunit_10n/port_arbiter1/np_ing0_head = gtgti1/gtgticcrunit1/gtgti_ccrunit_10n/port_arbiter1/np_ing0_head; I want... (11 Replies)
Discussion started by: pitagi
11 Replies

8. Shell Programming and Scripting

Count occurances of X Y Z in a file in 1 go.

Hi. I need to count multiple occurrences of X Y Z in a file in 1 go. At the moment I have the following scripts: ssh readonly@$ServerIP 'YEAR=xx;DAY=xx;MONTH=xx;LMONTH=xx;for i in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 \ 16 17 18 19 20 21 22 23; do cat... (13 Replies)
Discussion started by: msullivan
13 Replies

9. UNIX for Dummies Questions & Answers

occurances

Hi, Anyone know how to do the following? :eek: I Have a file as follows: happygoluckypeoplearenotalwayshappy happyisawordthatisnotusedalot If the word "happy" is present MORE than once in EACH line, I want to delete the line, hence in the above case, the first line will be deleted. ... (8 Replies)
Discussion started by: dr_sabz
8 Replies

10. Shell Programming and Scripting

Merging lines based on occurances of a particular character in a file

Hi, Is there any way to merge two lines based on specific occurance of a character in a file. I am having a flat file which contains multiple records. Each row in the file should contain specified number of delimiter. For a particular row , if the delimiter count is not matched with... (2 Replies)
Discussion started by: mohan_tuty
2 Replies
Login or Register to Ask a Question