linux - how to loop through 3 files , search for a specific string and match it to a string in another file using bash -
i hav 3 text files hav use following code. these : subscribers.txt ,towns.txt & calls.txt .their link http://www.cs.ub.bw/teaching/csi223 :
subscribers.txt has format:
callerid|name |gender|birthyear|hometownid|email 10000025|kgosi|f |1981 |33 |10000025@cs.ub.bw
town.txt has format :
hometownid|townname |location 28 |ramatlabama|b
calls.txt has format:
date |time |duration|calleeid|callerid|locationofcallee|locationofcaller 18/02/2005|19:25:27|257 |10000025|10000017|3 |28
i created function takes callerid parameter print out calls sent out callerid.how modify following code such read callerid keyboard,display calls of callerid.the first line before displaying calls must contain: callerid,name(from subsribers.txt),age(take birthyear subscribers.txt , calculate age),gender(from subscribers.txt)and townname(from towns.txt)
the code ::
#!/bin/bash echo "enter caller id " read x exec 401<> calls.txt # open calls.txt file exec 402<> subscribers.txt #opens subscribers.txt exec 403<> town.txt #opens tows.txt while read line <&402 #reading subscribers.txt { callerid=${line%%|*}; # cut off rest of $line keep callerid line=${line#*|}; name=${line%%|*}; # cut off rest of $line keep time of name line=${line#*|}; e gender=${line%%|*}; # cut off rest of $line keep box line=${line#*|}; year=${line%%|*}; # cut off rest of $line keep town line=${line#*|}; hometown=${line%%|*}; # cut off rest of $line keep phone line=${line#*|}; email=${line%%|*}; # cut off rest of $line keep email line=${line#*|}; if [ $callerid = "$x" ] echo $callerid "|" $name "|" $gender "|" $hometown ; echo " $callerid "|" $name "|" $gender "|" $hometown">>${x}outgoingcalls.log #stores in file fi } done exec 402>&- while read line <&401 # read line @ time calls.txt # if end of file reached, while yield false terminate { full_line=$line; # because $line going change, store somewhere date=${line%%|*}; # cut off rest of $line keep date line=${line#*|}; time=${line%%|*}; # cut off rest of $line keep time line=${line#*|}; duration=${line%%|*}; # cut off rest of $line keep duration line=${line#*|}; callee=${line%%|*}; # cut off rest of $line keep callee line=${line#*|}; caller=${line%%|*}; # cut off rest of $line keep phone line=${line#*|}; ### extract phone calleeloc=${line%%|*}; # cut off rest of $line keep callee location line=${line#*|}; ### extract phone callerloc=${line%%|*}; # cut off rest of $line keep caller location line=${line#*|}; if [ $caller = "$x" ]; echo $date"|"$time"|"$duration"|"$callee"|"$caller"|"$calleloc"|" $callerloc echo "$date:$time:$duration:$callee:$caller:$calleeloc:$callerloc>>${x}outgoingcalls.log #stores in file fi } done exec 401>&-
your script processing every single line of calls , subscribers before matching against given input. more sensible use grep find relevant lines before processing them.
#!/bin/bash echo -n "enter caller id: " read caller_id caller_details=$(grep -m 1 "$caller_id" subscribers.txt) caller_name=$(echo $caller_details | cut -d\| -f2) caller_age=$(($(date +%y)-$(echo $caller_details | cut -d\| -f4))) caller_town=$(grep -m1 "$(echo $caller_details | cut -d\| -f5)" towns.txt | cut -d\| -f2) printf "caller id: %s | name: %s | age: %s | town: %s\n" "$caller_id" "$caller_name" "$caller_age" "$caller_town" echo "===================================================================" printf "%-10s | %-8s | %-3s | %-16s | %-16s | %-16s | %s\n" "date" "time" "dur" "callee" "caller" "callee town" "caller town" call in $(awk -v id="$caller_id" -f'|' '$5==id' calls.txt); call_date=$(echo "$call" | cut -d\| -f1) call_time=$(echo "$call" | cut -d\| -f2) call_duration=$(echo "$call" | cut -d\| -f3) callee=$(grep -m 1 "$(echo "$call" | cut -d\| -f4)" subscribers.txt | cut -d\| -f2) callee_town=$(grep -m1 "$(echo "$call" | cut -d\| -f6)" towns.txt | cut -d\| -f2) caller_town=$(grep -m1 "$(echo "$call" | cut -d\| -f7)" towns.txt | cut -d\| -f2) printf "%s | %s | %-3s | %-16s | %-16s | %-16s | %s\n" "$call_date" "$call_time" "$call_duration" "$callee" "$caller_name" "$callee_town" "$caller_town" done
sample output:
$ ./test enter caller id: 10000010 caller id: 10000010 | name: daniel,r b | age: 34 | town: gaborone =================================================================== date | time | dur | callee | caller | callee town | caller town 01/06/2005 | 14:58:31 | 250 | makwati,t.m. | daniel,r b | gaborone | tonota 02/03/2005 | 16:31:37 | 622 | matshameko,t.c. | daniel,r b | francistown | w. mohembo 02/09/2005 | 10:58:28 | 295 | khetho,p | daniel,r b | gaborone | gaborone 03/01/2005 | 20:06:26 | 325 | tladi,og | daniel,r b | kang | gaborone 03/07/2005 | 15:18:40 | 263 | king,w | daniel,r b | nokaneng | gaborone etc...
Comments
Post a Comment