Count of occurrence in particular column of the file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count of occurrence in particular column of the file.
# 1  
Old 02-08-2016
Count of occurrence in particular column of the file.

Hi All,

let's say an input looks like:

Code:
C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11
----------------------------------
1|0123452|C501|Z|Z|Z|E|E|E|E|E|E|E
1|0156123|C501|X|X|X|E|E|E|E|E|E|E
1|0178903|C501|Z|Z|Z|E|E|E|E|E|E|E
1|0127896|C501|Z|Z|Z|E|E|E|E|E|E|E
1|0981678|C501|X|X|X|E|E|E|E|E|E|E


Third column having a value like C501. Now, I need to find out when the third column having value like C501,then need to check count of occurrence of value like 'Z' or 'X' or 'Y' value from C4 column to C11 column. Then need to print how many times occurred that particular value in each column.

Output should be like this.

Code:
C3,C4,C5,C6,C7,C8,C9,C10,C11
----------------------------------
C501|Z=3|Z=3|Z=3|E=5|E=5|E=5|E=5|E=5|3=5
        X=2 X=2 X=2

Kindly help me how to achieve this result.

Last edited by vbe; 02-08-2016 at 04:27 AM.. Reason: code not icode tags
# 2  
Old 02-08-2016
A bit strange that the header has comma separators but the data have pipes instead.

---------- Post updated at 10:38 ---------- Previous update was at 10:36 ----------

And, your desired output seems somewhat inconsistent...
# 3  
Old 02-08-2016
Apologies!it was typo error.
# 4  
Old 02-08-2016
And, what is 3=5 in the result?
# 5  
Old 02-08-2016
this is my input
Code:
C1|C2|C3|C4|C5|C6|C7|C8|C9|C10|C11
-------------------------------------------
1|0123452|C501|Z|Z|Z|E|E|E|E|E|E|E
1|0156123|C501|X|X|X|E|E|E|E|E|E|E
1|0178903|C501|Z|Z|Z|E|E|E|E|E|E|E
1|0127896|C501|Z|Z|Z|E|E|E|E|E|E|E
1|0981678|C501|X|X|X|E|E|E|E|E|E|E

Expected Output:

Code:
C3|C4|C5|C6|C7|C8|C9|C10|C11
----------------------------------
C501|Z=3|Z=3|Z=3|E=5|E=5|E=5|E=5|E=5|3=5
        X=2 X=2 X=2

# 6  
Old 02-08-2016
Try:
Code:
awk '
  NR>2 {
    A[$3]
    for(i=4; i<=NF; i++) {
      B[$i]
      T[$3,$i,i]++
    }
  } 
  END {
    for(i in A) 
      for(j in B) {
        s=i
        for(k=4; k<=NF; k++)
          s=s OFS j "=" ((i,j,k) in T ? T[i,j,k] : 0)
        print s
     }
   }
' FS=\| OFS=\| file

Output:
Code:
C501|E=0|E=0|E=0|E=5|E=5|E=5|E=5|E=5|E=5|E=5
C501|X=2|X=2|X=2|X=0|X=0|X=0|X=0|X=0|X=0|X=0
C501|Z=3|Z=3|Z=3|Z=0|Z=0|Z=0|Z=0|Z=0|Z=0|Z=0


--
Note: instead of s=s OFS j "=" ((i,j,k) in T ? T[i,j,k] : 0) one could use s=s OFS j "=" T[i,j,k]+0 but it uses more memory...

Last edited by Scrutinizer; 02-08-2016 at 06:33 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 02-08-2016
Thanks Scrutinizer. Have got the expected result. Thanks again for resolve the issue.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count occurrence of column one unique value having unique second column value

Hello Team, I need your help on the following: My input file a.txt is as below: 3330690|373846|108471 3330690|373846|108471 0640829|459725|100001 0640829|459725|100001 3330690|373847|108471 Here row 1 and row 2 of column 1 are identical but corresponding column 2 value are... (4 Replies)
Discussion started by: angshuman
4 Replies

2. UNIX for Dummies Questions & Answers

Code for exact match to count occurrence

Hi all, I have an input file as below. I would like to count the occurrence of pattern matching 8th field for each line. Input: field_01 field_02 field_03 field_04 field_05 field_06 field_07 field_08 TA T TA T TA TA TA... (3 Replies)
Discussion started by: huiyee1
3 Replies

3. UNIX for Dummies Questions & Answers

Count occurrence of string (based on type) in a column using awk

Hello, I have a table that looks like what is shown below: AA BB CC XY PQ RS AA BB CC XY RS I would like the total counts depending on the set they belong to: if search pattern is in {AA, BB, CC} --> count them as Type1 | wc -l (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

4. UNIX for Dummies Questions & Answers

Count occurrence of column3 in column2

count occurrence of column3 in column2 with awk Input: AA BB BB AA BB CC AA BB CC AA CC BB CC CC BB BB Output: AA BB 3 (1 Reply)
Discussion started by: aydj
1 Replies

5. Shell Programming and Scripting

Count occurrence of string in a column using awk

Hi, I want to count the occurrences of strings in a column and display as in example below: Input: get1 345 789 098 get2 567 982 090 fet4 777 610 632 get1 800 544 230 get1 600 788 451 get2 892 321 243 get1 673 111 235 fet3 789 220 278 fet4 768 222 341 output: 4 get1 345 789... (7 Replies)
Discussion started by: aydj
7 Replies

6. Shell Programming and Scripting

How to count the occurrence of a number?

Hi, I have a file which contained a set of numbers like Col1 col2 col3 col4 1 sa 13 0 2 sb 14 0 3 sc 15 9 4 sd 16 -9 5 sd 20 -2 6 sd 20 4 Here in last column I need to count the zeros, positive values and negative values, please help me to do that. (2 Replies)
Discussion started by: Shenbaga.d
2 Replies

7. Shell Programming and Scripting

Count number of occurrence of a string in file

if there's a file containing: money king money queen money cat money also money king all those strings are on one line in the file. how can i find out how many times "money king" shows up in the line? egrep -c "money king" wont work. (7 Replies)
Discussion started by: SkySmart
7 Replies

8. Shell Programming and Scripting

Count number of occurrence at each line

Hi I have the following file ENST001 ENST002 4 4 4 88 9 9 ENST004 3 3 3 99 8 8 ENST009 ENST010 ENST006 8 8 8 77 8 8 Basically I want to count how many times ENST* is repeated in each line so the expected results is 2 1 3 Any suggestion please ? (4 Replies)
Discussion started by: fuad_
4 Replies

9. Shell Programming and Scripting

Count no of occurrence of the strings based on column value

Can anyone help me to count number of occurrence of the strings based on column value. Say i have 300 files with 1000 record length from which i need to count the number of occurrence string which is existing from 213 to 219. Some may be unique and some may be repeated. (8 Replies)
Discussion started by: zooby
8 Replies

10. Shell Programming and Scripting

count a occurrence

I am looking to get a output of "2 apple found" from the awk command below. black:34104 tomonorisoejima$ cat tomo apple apple black:34104 tomonorisoejima$ awk '/apple/ {count++}END{print count " apple found"}' tomo 1 apple found black:34104 tomonorisoejima$ (5 Replies)
Discussion started by: soemac
5 Replies
Login or Register to Ask a Question