Count if numbers are not repetitive


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count if numbers are not repetitive
# 1  
Old 04-24-2007
Count once if numbers are repetitive

Hi All,

I have an input below and i would want to do a count on all the term "aaa" and count only once if the number in first column is the same.
For eg, if i use a "grep -c aaa input" command, the count will be "8".
However, i would want the count to be "6" instead since 2 numbers in the 1st column is repetitive and i wouldn;t want to do a double count. So can anybody show me how to edit the above grep command to get my desired output ?

Input:

111 aaa
111 aaa
222 aaa
333 aaa
444 aaa
444 aaa
555 aaa
666 aaa

Last edited by Raynon; 04-25-2007 at 12:02 AM..
# 2  
Old 04-25-2007
Code:
uniq file | grep -c "aaa"

If the data is not sorted then sort the file and pass to uniq command
# 3  
Old 04-25-2007
Hi anbu,

Thanks it works
# 4  
Old 04-25-2007
Hi Anbu,

What if i want to count only when there are 2 occurence?
For the below example, the count would be "2" in this case since "111 aaa" and "444 aaa" are repetitive and have 2 or more occurence.
# 5  
Old 04-25-2007
Quote:
Originally Posted by Raynon
Hi Anbu,

What if i want to count only when there are 2 occurence?
For the below example, the count would be "2" in this case since "111 aaa" and "444 aaa" are repetitive and have 2 or more occurence.
Code:
$ awk ' { arr[$0]++ } END { for( str in arr  ) { if ( arr[str] > 1 ) print str " " arr[str] } } ' file
111 aaa 2
444 aaa 2

# 6  
Old 04-25-2007
Hi Anbu,

I don;t mean that.
What i mean is that for the below case, there are 3 sets(as highlighted) with 2 occurences so the count will be 3.

For eg,

Input:
111 aaa
111 aaa

222 aaa
333 aaa
444 aaa
444 aaa

555 aaa
666 aaa
666 aaa

777 aaa
# 7  
Old 04-25-2007
Quote:
Originally Posted by Raynon
Hi Anbu,

I don;t mean that.
What i mean is that for the below case, there are 3 sets(as highlighted) with 2 occurences so the count will be 3.

For eg,

Input:
111 aaa
111 aaa

222 aaa
333 aaa
444 aaa
444 aaa

555 aaa
666 aaa
666 aaa

777 aaa
Code:
uniq -d file | grep -c "aaa"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count Repetitive Number in a column and renumbering using awk

Unable to get the desired output. Need only the rows which has repeated values in column 5. Input File <tab separated file> chr1 3773797 3773797 CEP10 1 chr1 3773797 3773797 CEP104 2 chr1 3689350 3689350 SMIM1 2 chr1 3773797 3773797 CEP4 3 chr1 3773797 3773797 EP104 ... (7 Replies)
Discussion started by: himanshu
7 Replies

2. Shell Programming and Scripting

Shell script count lines and sum numbers from multiple files

I want to count the number of lines, I need this result be a number, and sum the last numeric column, I had done to make this one at time, but I need to make this for a crontab, so, it has to be an script, here is my lines: It counts the number of lines: egrep -i String file_name_201611* |... (5 Replies)
Discussion started by: Elly
5 Replies

3. Shell Programming and Scripting

Script Shell: Count The sum of numbers in a file

Hi all; Here is my file: V1.3=4 V1.4=5 V1.1=3 V1.2=6 V1.3=6 Please, can you help me to write a script shell that counts the sum of values in my file (4+5+3+6+6) ? Thank you so much for help. Kind regards. (3 Replies)
Discussion started by: chercheur111
3 Replies

4. Shell Programming and Scripting

Count the occurances of numbers

I have a file as shown below. I need to count the unique occurrences of numbers in the first and second column only if the third column is <= 10. Otherwise print the corresponding numbers as zero. Thanks in advance!:) 58 80 40.74 76 80 9.78 76 80 8 12 6 9 30 28 8.23 45 12 ... (13 Replies)
Discussion started by: andreaalex
13 Replies

5. UNIX for Dummies Questions & Answers

How to count how many numbers in a certain range?

Hi I have a data file with two columns which looks like: 1 42 2 40 3 55 4 50 5 38 6 49 7 33 8 46 9 39 10 33 11 33 12 26 13 46 14 44 15 55 16 54 17 30 18 32 (7 Replies)
Discussion started by: marhuu
7 Replies

6. Shell Programming and Scripting

Grouping data numbers in a text file into prescribed intervals and count

I have a text file that contains numbers (listed from the smallest to the largest). For ex. 34 817 1145 1645 1759 1761 3368 3529 4311 4681 5187 5193 5199 5417 5682 . . (5 Replies)
Discussion started by: Lucky Ali
5 Replies

7. Shell Programming and Scripting

count numbers of matching rows and replace its value in another file

Hello all, can you help me in this problem, assume We have two txt file (file_1 and file_3) one is file_1 contains the data: a 0 b 1 c 3 a 7 b 4 c 5 b 8 d 6 . . . . and I need to count the lines with the matching data (a,b,..) and print in new file called file_2 such as the... (4 Replies)
Discussion started by: GoldenFalcon10
4 Replies

8. Shell Programming and Scripting

Count the decimal numbers with 6 precision

Hi guys, I have a single column file with millions of records. I want to count the number of records with 6 decimal precision. for ex: 1234.12 1234.132 12345.12345 1234.1 1234.13 1234.123456 243435.454555 i need to count the number of records with precision of 6 ( i.e here the... (4 Replies)
Discussion started by: marcus_kosaman
4 Replies

9. Shell Programming and Scripting

to count their numbers using awk

suppose u have a file AAG AAG GYG HYH GTG AAG AAG HYH GYG HUH JUJ JUJ AAG so output shud have AAG = 5 GYG=2 HYH=2 HUH=1JUJ=2 (3 Replies)
Discussion started by: cdfd123
3 Replies

10. UNIX for Dummies Questions & Answers

Repetitive Tasks

Could someone tell me how I can simplify the script that follows!!! I know that there must be a way how to grep Average from sar01.................. sar02 ....................... sar03....................... sar04... (3 Replies)
Discussion started by: JairGuerra
3 Replies
Login or Register to Ask a Question