Count frequency of unique values in specific column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count frequency of unique values in specific column
# 1  
Old 01-27-2014
Count frequency of unique values in specific column

Hi, I have tab-deliminated data similar to the following:

Code:
dot is-big 2
dot is-round 3
dot is-gray 4
cat is-big 3
hot in-summer 5

I want to count the frequency of each individual "unique" value in the 1st column. Thus, the desired output would be as follows:

Code:
dot 3
cat 1
hot 1

is there an
Code:
awk

that could help me out with this?
Thank you!
# 2  
Old 01-27-2014
Code:
$ awk '{A[$1]++}END{for(i in A)print i,A[i]}' file

hot 1
cat 1
dot 3

# 3  
Old 01-27-2014
Hello,

Here is the solution for same.

Code:
awk '{a[$1]++;} END{for(i in a) print a[i]"  "i}' file_name

Output will be as follows.

Code:
1  hot
3  dot
1  cat



Thanks,
R. Singh
# 4  
Old 01-27-2014
try.

Code:
cut -f1 -d" " file | uniq -c
      3 dot
      1 cat
      1 hot

or with awk as requested

Code:
awk -F" " '{print $1}' tmp1 | uniq -c
      3 dot
      1 cat
      1 hot

---------- Post updated at 02:23 PM ---------- Previous update was at 01:53 PM ----------
# 5  
Old 01-27-2014
That would work only with the keywords in contiguous lines.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 01-27-2014
Quote:
Originally Posted by RudiC
That would work only with the keywords in contiguous lines.
yes, thank you,,, there should be
Code:
| sort

for non-contiguous keywords.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

2. UNIX for Beginners Questions & Answers

Count unique column

Hello, I am trying to count unique rows in my file based on 4 columns (2-5) and to output its frequency in a sixth column. My file is tab delimited My input file looks like this: Colum1 Colum2 Colum3 Colum4 Coulmn5 1.1 100 100 a b 1.1 100 100 a c 1.2 200 205 a d 1.3 300 301 a y 1.3 300... (6 Replies)
Discussion started by: nans
6 Replies

3. Shell Programming and Scripting

Print count of unique values

Hello experts, I am converting a number into its binary output as : read n echo "obase=2;$n" | bc I wish to count the maximum continuous occurrences of the digit 1. Example : 1. The binary equivalent of 5 = 101. Hence the output must be 1. 2. The binary... (3 Replies)
Discussion started by: H squared
3 Replies

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

5. Shell Programming and Scripting

Count Unique values from multiple lists of files

Looking for a little help here. I have 1000's of text files within a multiple folders. YYYY/ /MM /1000's Files Eg. 2014/01/1000 files 2014/02/1237 files 2014/03/1400 files There are folders for each year and each month, and within each monthly folder there are... (4 Replies)
Discussion started by: whegra
4 Replies

6. Shell Programming and Scripting

Count specific column values

Hi all: quick question! I have the following data that resembles some thing like this: i am tired tired am i what is up hello people cool I want to count (or at least isolate) all of the unique elements in the 2nd column. I have tried this: cut -f 2 | uniq 'input' which does... (3 Replies)
Discussion started by: owwow14
3 Replies

7. UNIX for Dummies Questions & Answers

How to count specific columns and merge with unique ones?

Hi. I am not sure the title gives an optimal description of what I want to do. I have several text files that contain data in many columns. All the files are organized the same way, but the data in the columns might differ. I want to count the number of times data occur in specific columns,... (0 Replies)
Discussion started by: JamesT
0 Replies

8. Shell Programming and Scripting

How to count Unique Values from a file.

Hi I have the following info in a file - <Cell id="25D"/> <Cell id="26A"/> <Cell id="26B"/> <Cell id="26C"/> <Cell id="27A"/> <Cell id="27B"/> <Cell id="27C"/> <Cell id="28A"/> I would like to know how would you go about counting all... (4 Replies)
Discussion started by: Prega
4 Replies

9. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies
Login or Register to Ask a Question