LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-31-2006, 07:29 AM   #1
sarajevo
Member
 
Registered: Apr 2005
Distribution: Debian, OpenBSD,Fedora,RedHat
Posts: 228
Blog Entries: 1

Rep: Reputation: 31
Counting number of occurance in awk program


Hi all, I am hoping that someone will have time to read about my problem and eventually write down some answer

I have some huge file and I want to check it on some values so I wrote script bellow
###################################################################
cat 942prm_$d | tr ' | ' ' ' > prm942_$d.txt # to translate | into empty space
d=`TZ=MET+24 date +%Y%m%d` # date

# And awk rule...

awk ' { if ( ($10==50005) && ($22==128) && ( $12~/^61/) || ($12~/^62/) && length($12) <= 8) print $10; print $12 }' prm942_$d.txt > mobilna.txt

awk ' { if ( ($10==50006) && ($22==128) && ( $12~/^61/) || ($12~/^62/) && length($12) <= 8) print $10; print $12 }' prm942_$d.txt > mobilna.txt

awk ' { if ( ($10==50007) && ($22==128) && ( $12~/^61/) || ($12~/^62/) && length($12) <= 8) print $10; print $12 }' prm942_$d.txt > mobilna.txt
#######################################################################

if colummn $10 is 50005 and $22 is 128 and $12 begins with 61 or 62 and length of that line is not smaller that 8 I need to print colummn $10 and $12 and I made it with above awk command. Where is column $1 represent called number and column $12 is number who called number in $1. $12------>$10 ( direction of call )
Now I need just small hint ( help ) how count how many times each case occur. I need output like this,

50005---------------4 ( means: the awk rule was ok 4 times for number 50005 )
50006---------------5 ( as above, just five times for number 50006)
50007---------------6 ( as above, just six times for number 50007)

in file mobilna.txt.
In other words how many times some customer which phone number begins with 61 or 62 called number 50005—first awk command, number 50006---second awk command, 50007 --- third awk command and so on.
What I want to say, if rules in awk command

awk ' { if ( ($10==50005) && ($22==128) && ( $12~/^61/) || ($12~/^62/) && length($12) <= 8) print $10; print $12 }' prm942_$d.txt > mobilna.txt

occurs some number of times for number 50005, I need to get output like above. I need to know how many time occurs that ( that is number of calls on that number ). I probably need some kind of counter but I do not know how implement that.
Every help or hint is welcome.
Thanks
 
Old 10-31-2006, 08:29 AM   #2
card-suse
Member
 
Registered: Dec 2005
Location: Ohio, USA
Distribution: OpenSuSE 10.2
Posts: 74

Rep: Reputation: 15
awk has the ability to do arrays. Indices for arrays in awk are actually characters. So you can define an array like:

Code:
++Counter[$10]
This should simultaneously allocate and increment the counter array if it doesn't already exist. I'm not sure if awk initializes the value to zero and I don't have the time right now to check. It it doesn't, then you need to initialize everything in a BEGIN block. You'll have to put your awk code in a file since it will take a few lines to do what you want to do. But here's an example (make a file called "myprog.awk"):


Code:
# This is the BEGIN block that initializes stuff

BEGIN{
Counter[50005]=0;
}

# Here is your conditional statement

if ( ($10==50005) && ($22==128) && ( $12~/^61/) || ($12~/^62/) && length($12) <= 8) 
{
  Counter[50005]+=1
}

# This is the END block that will output the results

END{
for (i in Counter) 
{
  printf("%10d--------%10d",i,Counter[i]
}
You could then run this file with:

Code:
awk -f mycode.awk prm942_$d.txt
That should do it....

Last edited by card-suse; 10-31-2006 at 08:39 AM.
 
Old 11-01-2006, 10:31 AM   #3
card-suse
Member
 
Registered: Dec 2005
Location: Ohio, USA
Distribution: OpenSuSE 10.2
Posts: 74

Rep: Reputation: 15
Well, did that work for you???????
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Value counting in awk scripts sarajevo Programming 9 10-20-2006 08:24 AM
Counting number of occurences of a certain chacter in string java linuxmandrake Programming 4 04-27-2006 11:10 PM
counting number of sheets printed from a printe avirup dasgupta Linux - General 2 07-25-2005 07:36 PM
counting number of files akin81 Linux - Newbie 6 03-25-2004 01:53 PM
counting the commented lines using awk [ /* */] itsjvivek Linux - General 8 01-17-2003 08:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration