-2

I have the file calls.txt. It has 7 fields:

Date|time|duration|callee|caller|calleeLocation|callerLocation
01/01/2005|15:55:27|495|10000075|10000029|29|4
01/01/2005|11:04:00|597|10000064|10000078|25|4
01/01/2005|08:44:06|593|10000070|10000107|1|7
01/01/2005|18:35:19|235|10000017|10000036|7|14

I want some help me to do the following methods:

  • that will print the caller who has called 10000027 more than anybody else

  • that will the caller who has called 10000027 longer than anybody else(using duration field).

  • how many calls 10000027 has made between 1st April 2005 and 31st April 2005

I tried some methods, but they don't do what I desire. This is my code:

#!/bin/bash
exec 401<> calls.txt 
while read line <&401      # read a line at a time from calls.txt
do                         # if end of file reached, while will yield false the$
{

full_line=$line;       # because $line is going to change, store it somewhe$


    date=${line%%|*}; # cut off the rest of $line but keep date 
    line=${line#*|};       


    time=${line%%|*}; # cut off the rest of $line but keep time
    line=${line#*|};       

    duration=${line%%|*};  # cut off the rest of $line but keep box
    line=${line#*|};       



    callee=${line%%|*};   # cut off the rest of $line but keep callee
    line=${line#*|};      



    caller=${line%%|*};   # cut off the rest of $line but keep caller
    line=${line#*|};      


    calleeLoc=${line%%|*};   # cut off the rest of $line but keep callee location
    line=${line#*|};


    callerLoc=${line%%|*};   # cut off the rest of $line but keep caller location
    line=${line#*|};

this method is supposed to print the caller who has called 10000027 most

    if [ $callee = 10000027 ]
     then  
       count= $(grep -cw $caller {callee}calls.txt
              if [[ $max_count -le $count ]] 
                 then
                max_count=$count;
                most_caller=$caller;
              fi
   fi  

how can i modify this method so that it print the caller who has the highest duration time among those who called 10000027

 if [ $callee = 10000027 ]
     then  
       count= $(grep -cw $caller {callee}calls.txt
              if [[ $max_count -le $count ]] 
                 then
                duration=$count;
                longest_caller=$caller;
              fi
   fi  

how can i modify this method so that it print how many calls made by 10000027 between 1st april 2005 and 31st april 2005

   if [ $caller = 10000027 ];
      then

            if [ $date -gt (1/4/2005) && $date -lt (31/4/2005) ];

                  $awk '$4~/10000027/{++c} END{ print c} 'FS=:calls.txt

            fi
    fi



}
done


exec 401>&-
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
tonto
  • 33
  • 7
  • 1
    can you properly specify what you want? because from your description regarding to method i can't get you what you actually want. – Jayesh Bhoi Mar 15 '14 at 13:19
  • @jayesh i want help with three methods. 1)the first one should print the callerId which called 10000027 more than any other callerID 2)the second method should print the callerId which had called 10000027 longer than any other callerID(using duration field) 3)the third method should print the number of calls made by 10000027 between 1st and 31st april 2005 – tonto Mar 15 '14 at 13:21
  • 1
    do you have to use shell processing for this? Awk or perl are much better suited to this sort of problem. If you must use shell, look at how the while loop can parse all var names in the solution for http://stackoverflow.com/questions/22384076/bash-script-while-loop-extremely-slow-read-file . You'd have to add IFS="|" for your data. Good luck. – shellter Mar 15 '14 at 14:02
  • If you'd like us to spend time helping you, please format your code to make it readable and explain exactly what it's not doing correctly. – Adam Liss Mar 15 '14 at 14:50

2 Answers2

1

You could try:

awk -f a.awk calls.txt

where a.awk is:

BEGIN {FS="|"; num="10000027"}
{
    if ($4==num) a[$5]++
}

END {
    max=0;
    for (i in a) {
        if (a[i]>max) {
            max=a[i]
            c=i;
        }
    }
    print "Caller: "c" called "num" "max" times."
}

Output:

Caller: 10000093 called 10000027 9 times.
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • @tonto Great :) Can you resolve the other questions? Just ask if you get stuck.. – Håkon Hægland Mar 15 '14 at 15:41
  • can u pliz help me with other questions .how do i equate a.awk to that code below? – tonto Mar 17 '14 at 08:12
  • @tonto Hi. Do you want to sum all durations, or just want to check if a given duration is the largest? For instance, if calller 10000093 calls 10000027 once for 800 secs, and once for 600 sec. Do you want to sum those, or just check if 800 secs is the largest? – Håkon Hægland Mar 17 '14 at 09:08
  • i want to loop through all those who durations who called 10000027 and get the highest duration – tonto Mar 17 '14 at 12:37
  • @tonto So you want to sum the duration of all the calls? You are not looking for a single longest call, but you are looking for the caller whose sum of durations of all calls he made to the given number, is the largest among the different callers? – Håkon Hægland Mar 17 '14 at 12:42
  • let me clarify the question:i want to get all those callers who called 10000027 then,check the caller who has the highest duration on the call made to 10000027 @hakon – tonto Mar 17 '14 at 13:26
  • @tonto Sorry, I did not see that you also asked how to run the code: Just save the code to a file `a.awk`. – Håkon Hægland Mar 17 '14 at 13:29
  • @tonto Here is an update to the code: http://pastebin.com/HeYJ66SX . It also finds the longest duration. – Håkon Hægland Mar 17 '14 at 13:38
  • i want to paste the code into another file so i cant rename the file to **a.awk** .how do i modify the code so that it works in another file @hakon – tonto Mar 17 '14 at 15:59
  • @tonto I am not sure what you mean. You can change the name to `b.awk` if you want.. – Håkon Hægland Mar 17 '14 at 18:20
0

This is a comment, but formatting comments is hard so I'm using the answer box. It is much easier to read the input using read:

while IFS=\| read Date time duration callee caller calleeLocation callerLocation
do
 ...
done < input-file

will do all the assignments for you and you can avoid the parsing that you are doing.

William Pursell
  • 204,365
  • 48
  • 270
  • 300