The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
UNIX newbie NEWBIE question! Hanamachi UNIX for Dummies Questions & Answers 4 03-28-2009 04:10 PM
Count not correct popeye Shell Programming and Scripting 3 04-22-2008 01:50 PM
How to count the record count in an EBCDIC file. oracle8 UNIX for Dummies Questions & Answers 1 07-26-2006 08:22 PM
CPU count hshapiro UNIX for Dummies Questions & Answers 2 04-03-2006 02:08 PM
count fields sskb UNIX for Dummies Questions & Answers 4 12-07-2001 12:40 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-17-2006
britney britney is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 8
Count help - newbie

I have a flat file sort by Phonenumber and Bigin call fileA.txt
Phonnumber|Begin|Endca|D1|D2|Diff
4159061234|10:00|10:01|a1|a2|60
4159061234|10:00|10:06|b1|b2|360

4159061234|10:05|10:06|c1|c2|60
4159061234|10:12|10:15|d1|d2|180
3045678934|10:25|10:28|x1|x2|180
3045678934|10:25|10:30|y1|y2|300

3045678934|10:28|10:31|z1|z2|180
....................

How do I write a code in ksh so it will check the
phone number and if phone number with same begin call then count
and give outout.txt as a result

Phonnumber|Begin|Endca|D1|D2|Call Times
3045678934|10:25|10:28|x1|x2|2
3045678934|10:28|10:31|z1|z2|1
4159061234|10:00|10:01|a1|a2|2
4159061234|10:05|10:06|c1|c2|1
4159061234|10:12|10:15|d1|d2|1

......................................

Thanks

===


Here is my code
Quote:
grep "|" fileA.txt | sort -t"|" +0 -1 +2 -3 +5 -6 |
awk -F"|" '{

ani=$1;
betime=$2;
if ((ani != ani0) && (betime != betime0)) {
any0 = ani;
betime0 = betime;
date=$2;
entime=$3;
D1=$4
D2=$5;
diff=$6;
count = "0";
if ( diff <= 600 ) {
count = count + 1;
printf "%s,%s,%s,%s,%s,%s\n",ani,betime,entime,D1,D2,count >> output.txt
}
}
}'
but my asnswer is
3045678934,10:25,10:28,x1,x2,1
3045678934,10:28,10:31,z1,z2,1
4159061234,10:00,10:01,a1,a2,1
4159061234,10:05,10:06,c1,c2,1
4159061234,10:12,10:15,d1,d2,1

Last edited by britney; 03-17-2006 at 08:16 PM..
  #2 (permalink)  
Old 03-17-2006
britney britney is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 8
I tried but my count is incorrect, how do you count it ? please help. Thanks
  #3 (permalink)  
Old 03-18-2006
RTM's Avatar
RTM RTM is offline Forum Advisor  
Hog Hunter
  
 

Join Date: Apr 2002
Location: On my motorcycle
Posts: 3,039
Suggest you first read the rules about bumping your post - this will not get you a quicker response.
  #4 (permalink)  
Old 03-20-2006
thestevew thestevew is offline
Registered User
  
 

Join Date: Mar 2006
Location: South Yorkshire, UK
Posts: 114
Quick and dirty solution if the file isn't too big:


Code:
IFS='|'
while read a b
do
  print $a"|$b"'|'$(grep -c ^$a z.dat)
done < z.dat

Sets the field separator to the pipe character then for each line print the original plus count of matches on the first field. Pro - simple, and no need for variables to hold counts or to sort the input, Anti - a grep for each line instead of for each unique value. You pays your money and takes your choice .

cheers
  #5 (permalink)  
Old 03-20-2006
britney britney is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 8
The code you have gave me this answer

4159061234|10:00|10:01|a1|a2|60|4
4159061234|10:00|10:06|b1|b2|360|4
4159061234|10:05|10:06|c1|c2|60|4
4159061234|10:12|10:15|d1|d2|180|4
3045678934|10:25|10:28|x1|x2|180|3
3045678934|10:25|10:30|y1|y2|300|3
3045678934|10:28|10:31|z1|z2|180|3



It is not what I expected to get.
Thanks,
  #6 (permalink)  
Old 03-20-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
nawk -f brit.awk fileA.txt

brit.awk:

Code:
BEGIN {
  FS=OFS="|"
}

{
  idx= $1 SUBSEP $2
  NF--; $1 = $1
  if (!(idx in arr))
     arr[ idx ] = $0
  cnt [idx]++
}
END {
  for ( i in arr )
    print arr[i], cnt[i]
}

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0