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
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
Quick and dirty solution if the file isn't too big:
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 .
vgersh99, your code is working but can you show me why mine did not work ?
I aslo add if diff < 600 then get the record, because I dont want to see any diff greater than 600 in there. Would you please show me how to fix my code to make it work like yours ? Thanks
sorry I don't have time to understand your code.
I've changed mine for the 'diff' logic':
BEGIN {
FS=OFS="|"
}
{
if ( int($NF) >= 600 ) next
idx= $1 SUBSEP $2
NF--; $1 = $1
if (!(idx in arr))
arr[ idx ] = $0
cnt [idx]++
}
END {
for ( i in arr )
print arr, cnt
}
Sorry Britney - didn't read the question properly :o :o :o
I can't improve on the awk solutions you've already had. Your requirement is a bit too subtle for the usual solutions for this sort of problem using sort/uniq etc and a straight ksh script (looping through, comparing keys and counting duplicates then printing on change of key) works fine but is pretty inelegant.
Sorry, newbir writing code
vgersh99, your code work great but it show array
for ( i in arr )
print arr[i], cnt
[i]That's mean all field then comma count
what if I need to rearrange like add count in 3rd field and remove diff=$6 from your code how do i do it ? I am sure you will not save to another file and do awk again to rearange it .
Thanks,