gpt4 book ai didi

linux - 如何循环遍历 3 个文件,搜索特定字符串并使用 bash 将其与另一个文件中的字符串匹配

转载 作者:太空宇宙 更新时间:2023-11-04 11:13:03 25 4
gpt4 key购买 nike

我有 3 个文本文件,我必须将其用于以下代码。它们是:subscribers.txt、towns.txt 和 calls.txt。 their link is http://www.cs.ub.bw/teaching/csi223 :

Subscribers.txt 的格式为:

 callerID|name |gender|birthYear|hometownID|email
10000025|kgosi|F |1981 |33 |10000025@cs.ub.bw

Town.txt 的格式为:

hometownID|townName   |location
28 |Ramatlabama|B

calls.txt 的格式为:

date      |time    |duration|calleeID|callerID|locationOfcallee|locationOfcaller
18/02/2005|19:25:27|257 |10000025|10000017|3 |28

我创建了一个函数,该函数将来电显示作为参数,然后打印出该来电显示发出的调用。我如何修改以下代码,使其从键盘读取来电显示,显示该来电显示的所有调用。显示所有调用之前的第一行必须包含:调用者 ID、姓名(来自 subsribers.txt)、年龄(从 subscribers.txt 中获取出生年份并计算年龄)、性别(来自 subscribers.txt)和 townName(来自 towns.txt)

代码是::

 #!/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 from subscribers.txt
do
{



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

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



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

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


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


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



if [ $callerid = "$x" ]
then

echo $callerid "|" $name "|" $gender "|" $hometown ;
echo " $callerid "|" $name "|" $gender "|" $hometown">>${x}outgoingcalls.log #stores in this file

fi

}
done
exec 402>&-


while read line <&401 # read a line at a time from calls.txt
do # if end of file reached, while will yield false then terminate
{
full_line=$line; # because $line is going to change, store it somewhere


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 duration
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 phone
line=${line#*|};

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



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

if [ $caller = "$x" ];
then

echo $date"|"$time"|"$duration"|"$callee"|"$caller"|"$calleLoc"|" $callerLoc
echo "$date:$time:$duration:$callee:$caller:$calleeLoc:$callerLoc>>${x}outgoingcalls.log #stores in this file
fi


}
done


exec 401>&-

最佳答案

在匹配给定输入之前,您的脚本会处理每一行调用和订阅者。在处理它们之前使用 grep 找到相关行会更明智。

#!/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"

for call in $(awk -v id="$caller_id" -F'|' '$5==id' calls.txt); do
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

示例输出:

$ ./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...

关于linux - 如何循环遍历 3 个文件,搜索特定字符串并使用 bash 将其与另一个文件中的字符串匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22220282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com