Question

i want help regarding ext file manipulation in bash i have three files calls.txt, subscribers.txt ,towns.txt the link for the files i want help to write a method that will read caller_town from keyboard and then search for the names of subscribers which had made calls to caller_town.

it should print in this format :

subcriber name 1

  • call 1
  • call 2
  • call 3

subcriber name 2

  • call 1
  • call 2
  • call 3

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#*|};

caller_details=$(grep -m 1 "$caller_id" subscribers.txt)
caller_name=$(echo $caller_details | cut -d\| -f2)
caller_town=$(grep -m1 "$(echo $caller_details | cut -d\| -f5)" towns.txt | cut -d\| -f2)

i want help with this method because it print 1 call for each subscriber instead of printing more than one call

    if [ $caller = $callerID ]
     then  

       echo $caller_name;
       echo $date"|"$time"|"$duration"|"$callee"|"$caller"|"$calleeLoc"|"$callerLoc;
   fi  


}
done


exec 401>&-
Was it helpful?

Solution

I guess this this will roughly do the job. Try to combine with your own code, instead of just copy-pasting into your homework solution.

#!/bin/bash

echo -n "enter town: "
read caller_town

town_id=$(grep "|$caller_town|" towns.txt | awk -F '|' '{ print $1; }')

while read line; do
  caller_id=$(echo "$line" | awk -F '|' '{ print $1; }')
  egrep "\|$caller_id\|[0-9]+\|$town_id$" calls.txt > /dev/null
  if [ $? -eq 0 ]; then
    echo "$line" | awk -F '|' '{ print $2; }'
    egrep "\|$caller_id\|[0-9]+\|$town_id$" calls.txt
    echo
  fi
done < subscribers.txt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top