Finding occurences of numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding occurences of numbers
# 1  
Old 04-14-2011
Finding occurences of numbers

I have two files
The first file is in following format

Code:
 5	937 	 8
 1860	5	 1
 683	2 	 1
 129	2 	 2
 5	938 	 8
 1122	5	 1
 20	520 	 4
 1860	1851 	 1
 5	939 	 8
 1122	1124 	 1
 20	521 	 4
 5883	14 	 6
 1860	1852 	 1
 683	4 	 2


and the second file consists of the following fields
Code:
 5	
 1860 
 683	 
 129		 
 1122	 	 
 20	
 1883

i.e. all unique fields from the first column of first field (I have obtained that)

now the problem is that i need to count the number of occurences of the number stored in second file in the first column of the first file

e.g.

Code:
for 5 in the second file, 
the count is 4

and for 20 the count is 2

for  1860 count is 3
for 129 count is 1

how can i deal with this ?

thanks in advance
# 2  
Old 04-14-2011
Code:
#!/bin/bash

DATA1=file1.txt
DATA2=file2.txt

awk 'BEGIN {
while ((getline < "'$DATA2'") > 0)
        f2array[$1] = "foo"; 
}

{
results[$1]+=0
results[$2]+=0
results[$3]+=0
if ( $1 in f2array ) 
 results[$1]++
if ( $2 in f2array ) 
 results[$2]++
if ( $3 in f2array ) 
 results[$3]++

}

END{
 for (var in results)
  print var " has " results[var] " occurrences"

}' $DATA1

the result:
Code:
521 has 0 occurrences
1124 has 0 occurrences
4 has 0 occurrences
683 has 2 occurrences
129 has 1 occurrences
937 has 0 occurrences
5 has 5 occurrences
6 has 0 occurrences
938 has 0 occurrences
939 has 0 occurrences
8 has 0 occurrences
20 has 2 occurrences
5883 has 0 occurrences
1851 has 0 occurrences
1860 has 3 occurrences
1852 has 0 occurrences
14 has 0 occurrences
1 has 0 occurrences
1122 has 2 occurrences
2 has 0 occurrences
520 has 0 occurrences




use the following if you want to exclude numbers that have no occurrences:
Code:
#!/bin/bash

DATA1=file1.txt
DATA2=file2.txt

awk 'BEGIN {
while ((getline < "'$DATA2'") > 0)
        f2array[$1] = "foo"; 
}

{
if ( $1 in f2array ) 
 results[$1]++
if ( $2 in f2array ) 
 results[$2]++
if ( $3 in f2array ) 
 results[$3]++

}

END{
 for (var in results)
  print var " has " results[var] " occurrences"

}' $DATA1


ths output:
Code:
683 has 2 occurrences
129 has 1 occurrences
5 has 5 occurrences
20 has 2 occurrences
1860 has 3 occurrences
1122 has 2 occurrences

This User Gave Thanks to |UVI| For This Post:
# 3  
Old 04-14-2011
Another one:

Code:
awk 'NR == FNR { _[$1]++; next }
$1 in _ {
  printf "for %s in file %s, \nthe count is %d\n", \
    $1, FILENAME, _[$1]
  }' file1 file2

This User Gave Thanks to radoulov For This Post:
# 4  
Old 04-14-2011
@radoulov, |UVI|

if exclude numbers that have no occurrences, we needn't the second file at all.

Code:
awk '{a[$1]++}END{for ( i in a) print i , a[i]}' infile

# 5  
Old 04-15-2011
Quote:
Originally Posted by rdcwayx
@radoulov, |UVI|

if exclude numbers that have no occurrences, we needn't the second file at all.

Code:
awk '{a[$1]++}END{for ( i in a) print i , a[i]}' infile

Sure,
but there might be numbers not to be reported (numbers present in the first file, but not present in the second file).
# 6  
Old 04-15-2011
The second file is not required in the awk command.
rdcwayx's command would suffice.

As per stuggler, second file has just unique vales of first file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

awk script for finding probability of distribution of numbers

Dear All I am having data file containing 0 to 40,000 like this... 0 5 1 65 2 159 3 356 ... ... 40000 19 I want to find the probability of distribution between the numbers. The second column values are angles from 0 to 360 and the 1st column is number of files. I am expecting... (2 Replies)
Discussion started by: bala06
2 Replies

2. Shell Programming and Scripting

finding distance between numbers

Hi, I have a file as ABC 1634230,1634284,1634349,1634468 1634272,1634301,1634356,1634534 What I want is to find distance between the numbers.. column 1 is the gene name and column 2 are starts and column 3 are their respective stops for the starts. So what I want is column 3 which has +1... (2 Replies)
Discussion started by: Diya123
2 Replies

3. Shell Programming and Scripting

finding lowest numbers

i want to basically get the lowest numbers from a list ... for example my input file is .... 1 2 3 6 7 8 9 10 11 13 Now i want to create a script or a one liner which i can use like this ... for example ..../getlowest 3 --> this gives me the next 3 lowest numbers which... (6 Replies)
Discussion started by: greycells
6 Replies

4. UNIX for Dummies Questions & Answers

Finding numbers in lines with strings and number and doing some manipulation

Hi, I want to write a script that does something like this: I have a file, in which in every line, there is a string of words, and followed by some space, a number. Now, I want to identify the line, which has the largest startFace number (say m=8118), take that number and add it to the... (2 Replies)
Discussion started by: super_commando
2 Replies

5. Shell Programming and Scripting

finding common numbers (contents) across 2 or 3 files

I have 3 files which are tab delimited and have numbers in it. file 1 1 2 3 4 5 6 7 File 2 3 5 7 8 File 3 1 (4 Replies)
Discussion started by: Lucky Ali
4 Replies

6. Shell Programming and Scripting

Finding the sum of two numbers

cat *.out |grep "<some text>" | awk '{print $6}' For ex,This will reutrn me 11111 22222 is it possible to add these two numbers in the above given command itself?I can write this to a file and find the sum. But I prefer to this calculation in the above given line itself. Any... (3 Replies)
Discussion started by: prasperl
3 Replies

7. UNIX for Dummies Questions & Answers

finding files with numbers in the file name???

Hello all, New to this forum! I got a Q: i want to find all files with numbers in the file name. e.g. blabla234.pm or fool654.pl action i took: ls | egrep '+' ls | egrep ls | egrep + ls | egrep '' ls | egrep '(+)' ls | egrep '()' ls | egrep '(.*.*)' ls | egrep '.*.*' ls | grep... (2 Replies)
Discussion started by: RedGrinGo
2 Replies

8. Shell Programming and Scripting

Finding all occurences in a file within the last 40 days

Morning, I have a database log file that i need to scan thru to find all successful logins within the last 40 days, What i have currently done is create a bash script to scan the last month and pipe out the results to a text file. YEAR=`date '+%Y'` MONTH=`date '+%m'` grep 'Login... (12 Replies)
Discussion started by: Jefferson333
12 Replies

9. Shell Programming and Scripting

Need help in finding and replacing port numbers.

Hi All, I am trying to write a shell script which firstly will search some files and then increase the port numbers mentioned in them by a certain no. let me clear it with an example- suppose there r few files a,b,c,d.... file a's content- <serverEntries xmi:id="ServerEntry_1"... (3 Replies)
Discussion started by: ankushsingh10
3 Replies

10. IP Networking

finding port numbers

hither! whatz the command to find which process is using a specific port number? for example, port 8082? (3 Replies)
Discussion started by: darkcastle
3 Replies
Login or Register to Ask a Question