How to count Unique Values from a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to count Unique Values from a file.
# 1  
Old 01-13-2011
How to count Unique Values from a file.

Hi

I have the following info in a file -
Code:
      <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 unique values within this file.I have limited knowledge but I think I should follow
these steps - Search the file for the occurance of the word "cell Id" than obtain the value "25D" than go through a conditon to see if value is unique and add to counter and display count.

I dont know were to start.

Any help or explanation would highly appreciative

Regards


Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples, thank you!

Last edited by Franklin52; 01-13-2011 at 05:17 AM..
# 2  
Old 01-13-2011
# 3  
Old 01-13-2011
Hi Anurag.

Many Tanks for the speedy response.I have seen this thread but i'm not sure how the loop works.Please could help me understand as I have 3 more question that is of similar type which I than can tackle.

awk '/^cell id/{if(!a[$NF]) cnt++;a[$NF]++;next}END{print cnt}' inputFile
# 4  
Old 01-13-2011
This should be sufficient:
Code:
awk '{a[$0]++}END{for(i in a)if(a[i]==1)print i}' file

# 5  
Old 01-13-2011
Use Franklin52's soln if input file is exactly like
Code:
<Cell id="Some_Value"/>

In case you input differs a little like
Code:
<Cell id="Value1"/>
<Cell id="Value1" dfd="ddfd" dfdsfdsf dff"/>

Then use soln in post #4 in above link which is:
Code:
awk -F\" '/Cell id/{if(!a[$2]) cnt++;a[$2]++;next}END{print cnt}' inputFile

It increments cnt value when a new Cell id value is found (Every Cell id value is stored in array once found and if a Cell id value is not found in array, means 1st occurance, then cnt is incremented).
Login or Register to Ask a Question

Previous Thread | Next Thread

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

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

4. Shell Programming and Scripting

Count frequency of unique values in specific column

Hi, I have tab-deliminated data similar to the following: 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: dot 3 cat 1 hot 1 is... (5 Replies)
Discussion started by: owwow14
5 Replies

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

6. Shell Programming and Scripting

Find and count unique date values in a file based on position

Hello, I need some sort of way to extract every date contained in a file, and count how many of those dates there are. Here are the specifics: The date format I'm looking for is mm/dd/yyyy I only need to look after line 45 in the file (that's where the data begins) The columns of... (2 Replies)
Discussion started by: ronan1219
2 Replies

7. Shell Programming and Scripting

List unique values and count instances in .csv file

I need to take the second column of a .csv file and count the number of instances of each unique value in that same second column. I'd like the output to be value,count sorted by most instances. Thanks for any guidance! Data example: 317476,317756,0 816063,318861,0 313123,319091,0... (4 Replies)
Discussion started by: batcho
4 Replies

8. Shell Programming and Scripting

Unique count from flat file

Hello Guys I have a flat file with '|~|' delimited When I use to record count using below command awk -FS"+" ' {print $colno}' filename | wc -l the count is fine But when I am trying to find the unique number of record the o/p is always 1 awk -FS"+" ' {print $colno}'... (11 Replies)
Discussion started by: Pratik4891
11 Replies

9. UNIX for Advanced & Expert Users

Count number of unique patterns from a log file

Hello Everyone I need your help in fixing this issue., I have a log file which has data of users logging in to an application. I want to search for a particular pattern in the log ISSessionValidated=N If this key word is found , the above 8 lines will contain the name of the user who's... (12 Replies)
Discussion started by: xtechkid
12 Replies

10. Shell Programming and Scripting

Getting Unique values in a file

Hi, I have a file like this: Some_String_Here 123 123 123 321 321 321 3432 3221 557 886 321 321 I would like to find only the unique values in the files and get the following output: Some_String_Here 123 321 3432 3221 557 886 I am trying to get this done using awk. Can someone please... (5 Replies)
Discussion started by: Legend986
5 Replies
Login or Register to Ask a Question