Sponsored Content
Top Forums Shell Programming and Scripting to count the number of occurences of a column value Post 302443279 by clx on Sunday 8th of August 2010 02:24:15 AM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

10. 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
/* Part of publib.

   Copyright (c) 1994-2006 Lars Wirzenius.  All rights reserved.

   Redistribution  and	use  in  source and binary forms, with or
without
   modification, are permitted provided that the following condi-
tions
   are met:

   1.  Redistributions of source code must retain the above copy-
right
      notice, this list of  conditions	and  the  following  dis-
claimer.

   2. Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials pro-
vided
      with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY  EX-
PRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IM- PLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSE- QUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTI- TUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. STRGSUB(3pub) C Programmer's Manual STRGSUB(3pub) */ NAME
strgsub - substitute all occurences of pattern with another string SYNOPSIS
#include <publib.h> int strgsub(char *str, const char *pat, const char *sub, size_t max); DESCRIPTION
strgsub finds all occurences of the pattern pat in the string str (using a method similar to strstr(3) to find the occurrences, i.e., no regular expressions), and replaces each with sub. If pat does not occur in str, no substitution is made. The size (including the terminat- ing '') of the string after the substitutions may be at most max chars. If it would be larger, no substitutions are made. Of course, if sub is an empty string, the occurences of the pattern are deleted from the string. RETURN VALUE
strgsub returns the number of substitutions made, or -1 if the result would not have fit into max chars. EXAMPLE
To substitute every "foo" with "bar" in a line, one might do the following. strgsub(line, "foo", "bar", strlen(line)+1); SEE ALSO
publib(3), strstr(3), strsub(3) AUTHOR
Lars Wirzenius (lars.wirzenius@helsinki.fi) Publib C Programmer's Manual STRGSUB(3pub)
All times are GMT -4. The time now is 08:55 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy