Question

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>&-
Was it helpful?

Solution 2

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.

OTHER TIPS

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top