Finding Sum of Occurrances


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding Sum of Occurrances
# 1  
Old 11-07-2010
Finding Sum of Occurrances

Hello friends,

I have a database export and with this export i need to find some statistics which is required. I take the export of a table with only 3 columns like this:

Code:
2nd      3rd         12th
123 05.11.2010 09:27 0
124 05.11.2010 09:28 0
125 05.11.2010 09:34 1
126 05.11.2010 09:39 0
127 05.11.2010 09:41 1
124 05.11.2010 09:44 0
126 05.11.2010 09:45 0
123 05.11.2010 09:49 0
124 05.11.2010 09:51 1
123 05.11.2010 09:54 1
126 05.11.2010 09:57 0
126 05.11.2010 09:57 1

Only the 2nd and 12th columns are important; 2nd column is subscribers and 12th is media result. If a subscriber listens media before their call connected then their media result is 1 otherwise 0. How could i find sum of subscribers who has listened the media (media result =1) in their first calls or second calls etc?
Output :
Code:
1st  2nd  3rd  4th
  2    0    2     1

3rd column of Subscribers 125 and 127 are "1" in first times so sum of them in 1st times is 2..

Is associative arrays of awk the right method here? As an average awk user im not good at in associative arrays, I need your help i couldnt find a similar thread and nothing else comes into my mind..

Regards

Last edited by EAGL€; 11-07-2010 at 07:13 AM.. Reason: adding info..
# 2  
Old 11-07-2010
something like this,

Code:
sort -n inputfile | awk '{if (p != $1) {j=0}a[$1]=+$4;b[$1]=++j;p=$1} END {print "1st\t2nd\t3rd\t4th"; one=two=three=four=0;for (i in a) {if(b[i]==1){one++};if(b[i]==2) {second++} ; if (b[i]==3) {three++} ; if (b[i]==4) {four++}} print one"\t"two"\t"three"\t"four}'


Last edited by pravin27; 11-07-2010 at 08:02 AM..
This User Gave Thanks to pravin27 For This Post:
# 3  
Old 11-07-2010
Pravin thats great it works like a charm; im going to apply it to my files. I should go deeper into awk associative arrays..

---------- Post updated at 03:35 PM ---------- Previous update was at 01:56 PM ----------

Sorry to say that i couldnt test the code well, now i've just addded some lines to sample input file and number of second occurances are not increased, for testing i used this sample
Code:
123 05.11.2010 09:27 0
124 05.11.2010 09:28 0
125 05.11.2010 09:34 1
126 05.11.2010 09:39 0
127 05.11.2010 09:41 1
128 05.11.2010 09:44 0
124 05.11.2010 09:44 0
126 05.11.2010 09:45 0
123 05.11.2010 09:49 0
124 05.11.2010 09:51 1
123 05.11.2010 09:54 1
126 05.11.2010 09:57 0
126 05.11.2010 09:57 1
128 05.11.2010 09:44 1

Simply i added "128 05.11.2010 09:44 0" and "128 05.11.2010 09:44 1" lines so that number of second occurance should be 1.

but it gave
Code:
1st     2nd     3rd    4th
2       0       2       1

# 4  
Old 11-07-2010
Sorry, Actually variable name is 'second' not 'two'

try this,

Code:
sort -n inputfile| awk '{if (p != $1) {j=0}a[$1]=+$4;b[$1]=++j;p=$1} END {print "1st\t2nd\t3rd\t4th"; one=second=three=four=0;for (i in a) {if(b[i]==1){one++};if(b[i]==2) {second++} ; if (b[i]==3) {three++} ; if (b[i]==4) {four++}} print one"\t"second"\t"three"\t"four}'

# 5  
Old 11-07-2010
Now it worked.I should've noticed this!, Thanks,
greetings from chandigarh Smilie
# 6  
Old 11-07-2010
You can also find JDBC and UNIXODBC tools that make delimited files into tables you can query.
http://en.wikipedia.org/wiki/Flat_fi...mplementations

Last edited by DGPickett; 11-07-2010 at 12:05 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get the sum?

hi Gurus, I have an extract as seen below: INPUT 2015-08-24 15:00:00.0 |TCSERVER01 |ServiceEventHandler |2283 2015-08-24 15:01:00.0 |TCSERVER01 |ServiceEventHandler |576 2015-08-24 15:02:00.0 |TCSERVER01 |ServiceEventHandler |833 2015-08-24 15:03:00.0 |TCSERVER01 |ServiceEventHandler... (6 Replies)
Discussion started by: reignangel2003
6 Replies

2. Shell Programming and Scripting

Need help in finding sum for values in 2 different fields

Hi there, I have 2 files in following format cat file_1 Storage Group Name: aaaa HBA UID SP Name SPPort ------- ------- ------ 0 21 Storage Group Name: bbbb HBA UID... (2 Replies)
Discussion started by: jpkumar10
2 Replies

3. Shell Programming and Scripting

How to sum these value.

Hi, Unix Gurus, I need sum values from a file. file format like: 0004004 0000817 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0004406 the result should be 459227 (817+45000 ... + 4406) anybody can help me out (7 Replies)
Discussion started by: ken002
7 Replies

4. UNIX for Dummies Questions & Answers

Getting the sum

I am trying to get the sum of the first column of a file. When I use the same method for other files it works just fine... for some reason for the file below it gives me an error that I don't understand... I tried looking at different lines of the file and tried different things, but I still... (7 Replies)
Discussion started by: cosmologist
7 Replies

5. Solaris

How to Sum

Hi I need to incorporate a 'sum' as follows into a script and not sure how. I have a variable per line and I need them to be summed, e.g below 1 23 1,456 1 1 34 46 How do I calculate the sum of all these numbers to ouptut the answer ( 1,562) Thanks in advance (3 Replies)
Discussion started by: rob171171
3 Replies

6. Shell Programming and Scripting

Print sum and relative value of the sum

Hi i data looks like this: student 1 Subject1 45 55 Subject2 44 55 Subject3 33 44 // student 2 Subject1 45 55 Subject2 44 55 Subject3 33 44 i would like to sum $2, $3 (marks) and divide each entry in $2 and $3 with their respective sums and print for each student as $4 and... (2 Replies)
Discussion started by: saint2006
2 Replies

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

8. Shell Programming and Scripting

sum

Hello everyone I need to write a script that sums numbers passed to it as arguments on the command line and displays the results. I must use a for loop and then rewrite it using a while loop. It would have to output something like 10+20+30=60 this is what I have so far fafountain@hfc:~$ vi sum... (1 Reply)
Discussion started by: Blinky85
1 Replies

9. Shell Programming and Scripting

sum(col) finding from a file

Hi I have an file which looks like country address phone amount sweden |address |phone | 10 | Singapo |address |phone | 20 | Italy-N |address |phone | 30 | denmar |address |phone | 40 | Here i need to do the sum(amount), how to do this in shell scripting Thanks Babu (11 Replies)
Discussion started by: ksmbabu
11 Replies

10. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies
Login or Register to Ask a Question