Counting occurences in column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting occurences in column
# 1  
Old 09-22-2012
Counting occurences in column

Hi guys!

I have a problem writing script that would convert this input

Quote:
F;R;G;P;E
F;R;G;P;E
F;R;G;P;E
F;R;G;P;E
F;R;G;P;E
F;R;G;P;E
L;P;V;R;R
A;L;L;P;V
A;L;L;P;V
T;I;K;P;K
T;I;K;P;K

into this output:

Quote:
D 0 0 0 0 0
E 0 0 0 0 6
K 0 0 2 0 2
R 0 6 0 1 1
H 0 0 0 0 0
T 2 0 0 0 0
S 0 0 0 0 0
N 0 0 0 0 0
Q 0 0 0 0 0
G 0 0 6 0 0
A 2 0 0 0 0
C 0 0 0 0 0
P 0 1 0 10 0
V 0 0 1 0 2
I 0 2 0 0 0
L 1 2 2 0 0
M 0 0 0 0 0
F 6 0 0 0 0
Y 0 0 0 0 0
W 0 0 0 0 0
B 0 0 0 0 0
Z 0 0 0 0 0
X 0 0 0 0 0
sum 11 11 11 11 11

I have an awk script that counts occurences of a sign in a column, but don't know how to change it so that I would give me desired output.

script
Code:
awk '{count[$1]++}END{for(j in count) print j";"count[j]""}' FS=: file.csv

I would appreciate any help!

Cheers,
grincz!

Last edited by grincz; 09-22-2012 at 09:28 AM.. Reason: spelling
# 2  
Old 09-22-2012
Assuming no empty field/line in the input, try:
Code:
awk -F\; '{for(i=1;i<=NF;i++) count[$i,i]++;if(max<NF) max=NF}
END{for(i=65;i<=90;i++) {
c=sprintf("%c",i);
printf("%s",c);
for(j=1;j<=max;j++)
 printf("\t%d",count[c,j])
print ""
}
printf ("sum\t%d\t%d\t%d\t%d\t%d",NR,NR,NR,NR,NR)
}' file

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 09-22-2012
Works just great! 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

Need help of counting no of column of a file

Hi All , I got stuck on the below scenario.If anyone can help me ,that will be really helpful. I have a target hdfs file layout.I need to know the no of column in that file. Target_RECRD_layout { ABC_ID EN NOTNULLABLE, ABC_COUNTRY CHARACTER ENCODING ASCII NOTNULLABLE, ... (5 Replies)
Discussion started by: STCET22
5 Replies

2. Shell Programming and Scripting

Counting a consecutive number in column 2

Hi, I have a input file which contains following data 0 1 0 2 0 3 0 4 0 8 0 9 0 11 1 1 1 2 1 6 1 7 1 8 1 9 2 1 2 11 2 12 (12 Replies)
Discussion started by: Ryan Kim
12 Replies

3. Shell Programming and Scripting

Counting the number of element in each column

Hello, I have a file as follows: ENSGALG00000000189 ENSGALG00000000189 ENSGALG00000000189 ENSGALG00000000215 ENSGALG00000000215 ENSGALG00000000218 ... (5 Replies)
Discussion started by: Homa
5 Replies

4. Shell Programming and Scripting

Help with awk in counting characters based on a column

Hello, I am using Awk in UBUNTU 12.04. I have a file as follows with 2172 rows and 44707 columns. ABO and GPO are the names of my populations. ABO_1 1 2 ABO_1 1 2 ABO_2 1 1 ABO_2 1 2 GPO_1 1 1 GPO_1 2 2 GPO_2 1 0 GPO_2 2 0I want to count the number of 1s and 2s in... (7 Replies)
Discussion started by: Homa
7 Replies

5. Shell Programming and Scripting

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 using the code cat file.txt | awk ' BEGIN { print "Category Counts"} {FS=","} {NR > 2} { cats = cats + 1} END { for(c in cats) { print c, "=", cats} } ' but its returning as ... (6 Replies)
Discussion started by: michaelrozar17
6 Replies

6. Shell Programming and Scripting

awk counting number of occurences

Hi, I am trying to count the max number of occurences of field1 in my apache log example: 10.0.0.1 field2 field3 10.0.0.2 filed2 field3 10.0.0.1 field2 field3 10.0.0.1 field2 field3 awk result to print out only the most occurence of field1 and number of occurence and field1 is... (3 Replies)
Discussion started by: phamp008
3 Replies

7. Shell Programming and Scripting

counting the number of occurences

say i've got a text file with >10million sequences: ssss ssss tttttt uuuuuu uuuuuu uuuuuu ... I'd like to convert the file so that the output will report the number of occurence right by each sequence: 2 ssss 2 ssss 1 tttttt 3 uuuuuu 3 uuuuuu 3 uuuuuu .... (3 Replies)
Discussion started by: johjoh
3 Replies

8. UNIX for Dummies Questions & Answers

Counting number of occurences

Hi All, I have to count the number of occurences of the character " ; " in a given line. I had used the following awk command to achieve the same echo $KOP.dat|awk '{split($1,my,";"); for(i in my)c++ }END{print c-1}' My file KOP.dat had the following data ... (1 Reply)
Discussion started by: kingofprussia
1 Replies

9. UNIX for Dummies Questions & Answers

Counting occurences of different strings in a file

Hi, i'd like to know if the following is possible with a shell script, and can't find the answer in the search. Suppose i have a logfile build like this: # 8 :riuyzp1028 # 38 : riuyzp1028 # 25 : riuyvzp1032 # 30 : nlkljpa0202 # 1 : nlklja0205 # 38 : riuyzp1028 # 25 :... (4 Replies)
Discussion started by: Freerider
4 Replies

10. UNIX for Dummies Questions & Answers

Counting occurences of specific charachter in a file

Hi, I need to count the number of occurences of the character " in a file that contains huge number of records. What command could I use? Please specify in detail since I am new :| Thanks much. (3 Replies)
Discussion started by: GMMike
3 Replies
Login or Register to Ask a Question