Awk: count unique elements in a field and sum their occurence across the entire file

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Awk: count unique elements in a field and sum their occurence across the entire file
# 1  
Old 04-18-2018
Awk: count unique elements in a field and sum their occurence across the entire file

Hi,

Sure it's an easy one, but it drives me insane.

input ("|" separated):
Code:
1|A,B,C,A
2|A,D,D
3|A,B,B

I would like to count the occurence of each capital letters in $2 across the entire file, knowing that duplicates in each record count as 1.
I am trying to get this output (tab-separated; does not matter if sorted or not by $1):
Code:
A   3
B   2
C   1
D   1


What I tried so far:
Code:
gawk '
BEGIN{FS="|";OFS="\t"}
{
   a=split($2,b,",")
   for(i=1;i<=a;i++){
      if(count[b[i]]++==0){
         total[b[i]]++
      }
   }
}
END{
   for(k in total){
      print k "\t" total[k]
   }
}


But I get:
Code:
A   1
B   1
C   1
D   1

There is a conflict between the array 'count' (to count the letter only once per field per record) and the array 'total' (to sum up the number of letter in the file).
# 2  
Old 04-18-2018
For my convenience, I rearranged / -idented your code, and added the delete count statement:
Code:
awk -F"|" -vOFS="\t" '

        {a = split ($2, b, ",")
         for (i=1; i<=a; i++)   if (!count[b[i]]++)     total[b[i]]++
         delete count
        }

END     {for (k in total)       print k "\t" total[k]
        }
' file
A    3
B    2
C    1
D    1

Looks promising!
This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-18-2018
I did not know the 'delete' function. It's great !

I did not know neither the form '!count[b[i]]++' instead of 'count[b[i]]++==0'

Thanks !
# 4  
Old 04-18-2018
You are using gawk so it was quite safe to assume it provides delete. Other awk versions don't necessarily, use split ("", ARR) then to clear array ARR.
awk and other (not all!) languages use integers to represent logical values: 0 and FALSE are interchangeable, as are 1 and TRUE (in fact, any non-null value will be treated as TRUE). Some people don't regard this "trick" justifiable...
This User Gave Thanks to RudiC For This Post:
# 5  
Old 04-18-2018
Code:
awk '
{for (i=2; i<=NF; i++) if (! line_fields[$i, NR]++) field_count[$i]++ }
END { for (i in field_count) print i "\t" field_count[i] }
' FS="[|,]" input

# 6  
Old 04-18-2018
Very useful information. Thanks !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

2. Shell Programming and Scripting

Count of unique lines in field 4

When I use the below awk to count the unique lines in $4 for the input it seems to work. The answer is 3 because $4 is only unique 3 times in all the entries. However, when I use the same on actual data I get 56,536 and I know the answer should be 56,548. My question is there a better way to... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

awk to count using each unique value

Im looking for an awk script that will take the unique values in column 5, then print and count the unique values in column 6. CA001011500 11111 11111 -9999 201301 AAA CA001012040 11111 11111 -9999 201301 AAA CA001012573 11111 11111 -9999 201301 BBB CA001012710 11111 11111 -9999 201301... (4 Replies)
Discussion started by: ncwxpanther
4 Replies

4. Shell Programming and Scripting

Looping through entire directory and count unique values

Hello, I`m a complete newbie to coding, please help with this problem. I have multiple files in a directory, I have to loop through the contents of each file and extract number of unique isoforms in that file. Each file is tab delimited and only the line with the first parent (column 3)... (1 Reply)
Discussion started by: ritakadm
1 Replies

5. Shell Programming and Scripting

awk sum entire string

Hi I am trying to carry out a sum on a file (totals.txt). The file looks like: So far i have this command this returns 20610 I however want it to return 000000206100 Any help would be great thanks! (6 Replies)
Discussion started by: nwalsh88
6 Replies

6. Shell Programming and Scripting

awk if statement not printing entire field

I have an input that looks like this: chr1 mm9_knownGene utr3 3204563 3206102 0 - . gene_id "Xkr4"; transcript_id "uc007aeu.1"; chr1 mm9_knownGene utr3 4280927 4283061 0 - . gene_id "Rp1"; transcript_id "uc007aew.1"; chr1 mm9_knownGene ... (5 Replies)
Discussion started by: pbluescript
5 Replies

7. Shell Programming and Scripting

awk and count sum ?

I have a input.txt file which have 3 fields separate by a comma place, os and timediff in seconds tampa,win7, 2575 tampa,win7, 157619 tampa,win7, 3352 dallas,vista,604799 greenbay,winxp, 14400 greenbay,win7 , 518400 san jose,winxp, 228121 san jose,winxp, 70853 san jose,winxp, 193514... (5 Replies)
Discussion started by: sabercats
5 Replies

8. Shell Programming and Scripting

Printing entire field, if at least one row is matching by AWK

Dear all, I have been trying to print an entire field, if the first line of the field is matching. For example, my input looks something like this. aaa ddd zzz 123 987 126 24 0.650 985 354 9864 0.32 0.333 4324 000 I am looking for a pattern,... (5 Replies)
Discussion started by: Chulamakuri
5 Replies

9. UNIX for Dummies Questions & Answers

How to search unique occurence in a file?

Hi, I have to search and count unique occurence of DE numbers in bold below in a file which has content like below. Proc Tran F-BUY Item Tkey Q5JV Item Tsid JTIZ9 Item Tdat 20091001 Item Tset 20091001 Item Tbkr 5 Item Tshs 2 Item Tprc 897.0 Item Tcom 2000.0 Item Tcm1 20091001... (6 Replies)
Discussion started by: akash028
6 Replies

10. Shell Programming and Scripting

Getting Sum, Count and Distinct Count of a file

Hi all this is a UNIX question. I have a large flat file with millions of records. col1|col2|col3 1|a|b 2|c|d 3|e|f 3|g|h footer**** I am supposed to calculate the sum of col1 1+2+3+3=9, count of col1 1,2,3,3=4, and distinct count of col1 1,2,3=c3 I would like it if you avoid... (4 Replies)
Discussion started by: singhabhijit
4 Replies
Login or Register to Ask a Question