to count the number of occurences of a column value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting to count the number of occurences of a column value
# 1  
Old 08-08-2010
to count the number of occurences of a column value

im trying to count the number of occurences of column 2 value(starting from KKK*) of the below file, file.txt
Quote:
Results of comparison of prod and swp table ran on 07142010
Client_ID,Category,Mrkt,PrdWk
4875,KKKPTOR,test,test # to start the count from here
4875,KKKPTOR,tst,tst3
4875,KKKTYUR,zz,zr
4875,KKKVBFF,zo,zt
4875,KKKPTOR,we,wy
4875,KKKSDSL,tt,te
4875,KKKDFSD,yy,yw
4875,KKKDFSD,yn,ya
4875,KKKTYUR,tt,te
4875,KKKTYUR,tt,te
4875,KKKTYUR,tt,te
4875,KKKSDSL,tt,te
4875,KKKDFSD,tt,te
using the code
Code:
cat file.txt | awk ' BEGIN { print "Category   Counts"} {FS=","} {NR > 2} { cats[$2] = cats[$2] + 1} END { for(c in cats) { print c, "=", cats[c]} } '

but its returning as
Quote:
Category Counts
KKKPTOR = 3
of = 1 # need to avoid this count
KKKTYUR = 4
KKKDFSD = 3
KKKSDSL = 2
KKKVBFF = 1
Category = 1 # need to avoid this count as well
how to avoid the script processing the line number 1 and 2 as you can see it returns "of = 1" and "Category = 1". Pls throw some light.Im using ksh.
# 2  
Old 08-08-2010
Code:
awk ' BEGIN { print "Category   Counts"} {FS=","} {if (/KKK/) { cats[$2] = cats[$2] + 1 }} END { for(c in cats) { print c, "=", cats[c]} } ' inputfile

OR
Code:
awk ' BEGIN { print "Category   Counts"} {FS=","} {if (NR>2) { cats[$2] = cats[$2] + 1 }} END { for(c in cats) { print c, "=", cats[c]} } ' inputfile

# 3  
Old 08-08-2010
May be,

Code:
$ awk -F, 'BEGIN {printf "Category\tCount\n" } $2 ~ /KKK/ {a[$2]++} END { for (i in a) {printf "%s\t\t%s\n",i , a[i] }}' file
Category        Count
KKKPTOR         3
KKKTYUR         4
KKKDFSD         3
KKKSDSL         2
KKKVBFF         1

# 4  
Old 08-08-2010
Hi,

try:

Code:
awk -F, 'BEGIN{print "Category \t Counts"}$2 ~ /^KKK/{a[$2]+=1}END{for(i in a){print i" =\t"a[i]}}' file

HTH Chris
# 5  
Old 08-08-2010
Code:
more +3 file | cut -d"," -f2 | sort|uniq -c

# 6  
Old 08-09-2010
Thank you all! All the posts helped me to get the count correct.
I have a doubt in the result that i got (my 1st post.)
Quote:
of = 1
. Why the awk command returned the above quoted as well.?Because wot i thot was as FS="," was used it only selects those lines which has comma (,) in it.
# 7  
Old 08-09-2010
Setting FS to "," on those lines which doesn't contains the "," means that the whole line will be considered as $1 ( first field ) so $1 and $0 would be same in that case.

This never means that the specific record would be ignored.
Code:
$ echo 'no FS' | awk -F, '{print $1"\n"$0}'
no FS
no FS

This User Gave Thanks to clx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count number of unique values in each column of array

What is an efficient way of counting the number of unique values in a 400 column by 1000 row array and outputting the counts per column, assuming the unique values in the array are: A, B, C, D In other words the output should look like: Value COL1 COL2 COL3 A 50 51 52... (16 Replies)
Discussion started by: Geneanalyst
16 Replies

2. Shell Programming and Scripting

Count and print the number of occurences

I have some files as shown below GLL ALM 654-656 654 656 SEM LYG 655-657 655 657 SEM LYG 655-657 655 657 ALM LEG 656-658 656 658 ALM LEG 656-658 656 658 ALM LEG 656-658 656 658 LEG LEG 658-660 658 660 LEG LEG 658-660 658 660 The value of GLL is... (5 Replies)
Discussion started by: arch
5 Replies

3. Shell Programming and Scripting

Count number of occurences using awk

Hi Guys, I have 2 files like below file1 xx yy file2 b yy b2 xx c1 yy xx yy Now I want an idea which can count occurences of text from file1 and file2 so outbout would be kind of (9 Replies)
Discussion started by: prashant2507198
9 Replies

4. 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

5. Shell Programming and Scripting

Count the number of fields in column

Hi I was going through the below thread https://www.unix.com/shell-programming-scripting/48535-how-count-number-fields-record.html I too have something similar requirement as specified in this thread but the number of columns in my case can be very high, so I am getting following error. ... (3 Replies)
Discussion started by: shekharjchandra
3 Replies

6. Shell Programming and Scripting

Count the number or row with same value in a column

This is the source file, we called it errorlist.out 196 server_a server_unix_2 CD 196 server_b server_win_1 CD 196 server_c server_win_2 CD 196 server_bd server_unix_2 CD 196 server_d server_unix_2 CD 196 server_es server_win_1 CD 196 ... (14 Replies)
Discussion started by: sQew
14 Replies

7. UNIX for Dummies Questions & Answers

how to count number of rows and sum of column using awk

Hi All, I have the following input which i want to process using AWK. Rows,NC,amount 1,1202,0.192387 2,1201,0.111111 3,1201,0.123456 i want the following output count of rows = 3 ,sum of amount = 0.426954 Many thanks (2 Replies)
Discussion started by: pistachio
2 Replies

8. Shell Programming and Scripting

Count number of occurences of a character in a field defined by the character in another field

Hello, I have a text file with n lines in the following format (9 column fields): Example: contig00012 149606 G C 49 68 60 18 c$cccccacccccccccc^c I need to count the number of lower-case and upper-case occurences in column 9, respectively, of the... (3 Replies)
Discussion started by: s052866
3 Replies

9. UNIX for Dummies Questions & Answers

Count number of occurences of a word

I want to count the number of occurences of say "200" in a file but that file also contains various stuff including dtaes like 2007 or smtg like 200.1 so count i am getting by doing grep -c "word" file is wrong Please help!!!!! (8 Replies)
Discussion started by: shikhakaul
8 Replies

10. Shell Programming and Scripting

How to count the number of occurences of this pattern?

Hi all, I have a pattern like this in a file: 123 4 56 789 234 5 67 789 121 3 56 789 222 4 65 789 321 6 90 100 478 8 40 789 243 7 80 789 How can I count the number of occurences of '789' (4th column) in this set...? Thanks for all your help! K (7 Replies)
Discussion started by: kripssmart
7 Replies
Login or Register to Ask a Question