[Solved] Awk: count occurrence of each character for every field


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Awk: count occurrence of each character for every field
# 1  
Old 03-03-2014
[Solved] Awk: count occurrence of each character for every field

Hi,

let's say an input looks like:
Code:
A|C|C|D
A|C|I|E
A|B|I|C
A|T|I|B

as the title of the thread explains, I am trying to get something like:
Code:
1|A=4
2|C=2|B=1|T=1
3|I=3|C=1
4|D=1|E=1|C=1|B=1

i.e. a count of every character in each field (first column of output) independently, sorted horizontally from the largest to smallest (like in output record 2 and 3 for instance).

I posted a similar question months ago (count number of distinct values in each column with awk | Unix Linux Forums | UNIX for Dummies Questions & Answers), and tried to adapt it as follow:
Code:
awk ' BEGIN {FS="|"}{for(i=1; i<=NF; i++){group[$i]++}} END{for(c in group) {print c " = " group[c]}}'

but it counts the character for the entire file and not independently for each field.

Any help would be greatly appreciated !

Thanks in advance !
# 2  
Old 03-03-2014
Try the below
Code:
awk 'BEGIN{FS = "|"; OFS = "|"}
  {for(i = 1; i <= NF; i++) {a[i " " $i]++}}
  END {for(x in a) {print x "=" a[x]}}' <INPUT_FILE> | awk '{if(a[$1] == "") {a[$1] = $1 "|" $2} else {a[$1] = (a[$1] "|" $2)}; if(max < $1) {max = $1}}
    END {for(i = 1; i <= max; i++) {print a[i]}}'

This User Gave Thanks to SriniShoo For This Post:
# 3  
Old 03-03-2014
SriniShoo provided a great start, but seems to have missed the requirement to sort the output on each line from highest count to lowest count. Here is a slight modification of SriniShoo's code that provides the requested sort order:
Code:
awk -F "|" '
{       for(i = 1; i <= NF; i++) {a[i " " $i]++} }
END {   for(x in a) print x " " a[x] }' input_file | 
sort -k3,3rn -k2,2 |
awk '
{       a[$1] = (a[$1] == "" ? $1 : a[$1]) "|" $2 "=" $3
        if(max < $1) max = $1
}
END {   for(i = 1; i <= max; i++) print a[i] }'

producing the output:
Code:
1|A=4
2|C=2|B=1|T=1
3|I=3|C=1
4|B=1|C=1|D=1|E=1

when given the sample input shown in message #1 in this thread.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 03-03-2014
Hi guy,
try this:
Code:
 awk '{for(i=1;i<=NF;i++){a[i]=a[i]?a[i]"|"$i:$i}}
 END{for(i=1;i<=length(a);i++){split(a[i],b);printf i;for(m in b){c[b[m]]++}for(n in c){printf "|"n"="c[n]}printf "\n";delete c}}' FS='|'   infile

This User Gave Thanks to Lucas_0418 For This Post:
# 5  
Old 03-03-2014
Thanks you guys !

Don Cragun's reply fits the best as it reverse sorts the count per line.

Perfect, thanks a lot !
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or other way to find out number of occurrence of 7th character

Hi all, I am looking for to filter out based on 7th character and list the number of occurrence based on the 7th character if p , d , o or m 1. if 7th character is p , Output should be: p_hosts = N 2. if 7th character is d , Output should be: d_hosts = N 3. if 7th character is o , Output... (10 Replies)
Discussion started by: rveri
10 Replies

2. Shell Programming and Scripting

awk to count condition in field

I am trying to confirm the counts from another code and tried the below awk, but the syntax is incorrect. Basically, outputting the counts of each condition in $8. Thank you :) awk '$8==/TYPE=snp/ /TYPE=ins/ /TYPE=del/ {count++} END{print count}'... (6 Replies)
Discussion started by: cmccabe
6 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. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 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

[solved]awk count occurences in time window

Input File Time, KeyStation 00:00:00,000;KS_1 00:00:01,000;KS_1 00:00:02,000;KS_1 00:00:03,000;KS_1 00:00:04,000;KS_1 00:00:05,000;KS_1 00:00:06,000;KS_1 00:01:01,000;KS_1 00:01:02,000;KS_1 00:01:03,000;KS_1 00:01:04,000;KS_1 00:01:05,000;KS_1 00:01:06,000;KS_1 01:00:01,000;KS_1... (0 Replies)
Discussion started by: Calypso
0 Replies

7. Shell Programming and Scripting

awk - count character count of fields

Hello All, I got a requirement when I was working with a file. Say the file has unloads of data from a table in the form 1|121|asda|434|thesi|2012|05|24| 1|343|unit|09|best|2012|11|5| I was put into a scenario where I need the field count in all the lines in that file. It was simply... (6 Replies)
Discussion started by: PikK45
6 Replies

8. UNIX for Dummies Questions & Answers

[Solved] Count amount of times of appearing of character before a word?

Hello Is there a way to calculate how many times a particular symbol appeared in a string before a particular word. Desktop/Myfiles/pet/dog/puppy So, I want to count number of occurence of"/" in this directory before the word dog lets say. Cheers, Bob (3 Replies)
Discussion started by: FUTURE_EINSTEIN
3 Replies

9. Shell Programming and Scripting

[Solved] Sed/awk print between patterns the first occurrence

Guys, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla bla 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla ... (6 Replies)
Discussion started by: ppolianidis
6 Replies

10. 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
Login or Register to Ask a Question