Linux 如何循环浏览3个文件,搜索特定字符串并使用bash将其与另一个文件中的字符串匹配
我有3个文本文件,我必须用于以下代码。它们是:subscribers.txt、towns.txt和calls.txt Subscribers.txt的格式为:Linux 如何循环浏览3个文件,搜索特定字符串并使用bash将其与另一个文件中的字符串匹配,linux,bash,delimiter,do-while,Linux,Bash,Delimiter,Do While,我有3个文本文件,我必须用于以下代码。它们是:subscribers.txt、towns.txt和calls.txt Subscribers.txt的格式为: callerID|name |gender|birthYear|hometownID|email 10000025|kgosi|F |1981 |33 |10000025@cs.ub.bw date |time |duration|calleeID|callerID|locationOf
callerID|name |gender|birthYear|hometownID|email
10000025|kgosi|F |1981 |33 |10000025@cs.ub.bw
date |time |duration|calleeID|callerID|locationOfcallee|locationOfcaller
18/02/2005|19:25:27|257 |10000025|10000017|3 |28
Town.txt的格式如下:
hometownID|townName |location
28 |Ramatlabama|B
calls.txt的格式为:
callerID|name |gender|birthYear|hometownID|email
10000025|kgosi|F |1981 |33 |10000025@cs.ub.bw
date |time |duration|calleeID|callerID|locationOfcallee|locationOfcaller
18/02/2005|19:25:27|257 |10000025|10000017|3 |28
我创建了一个函数,该函数将callerID作为参数,然后打印出该callerID发出的调用。如何修改以下代码,使其能够从键盘读取callerID,显示该callerID的所有调用。显示所有调用之前的第一行必须包含:callerID、name(来自subscribers.txt)、age(从subscribers.txt中获取出生年份并计算年龄)、性别(从subscribers.txt中)和汤名(从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>&-
!/bin/bash
回显“输入来电显示”
读x
exec 401 calls.txt#open calls.txt文件
exec 402 subscribers.txt#打开subscribers.txt
exec 403 town.txt打开tows.txt
在读取行>${x}outgoingcalls.log时,存储在此文件中
fi
}
完成
exec 402>&-
在read line中,您的脚本在与给定输入进行匹配之前正在处理每一行呼叫和订户。在处理它们之前,使用grep查找相关行更为明智
!/bin/bash
echo-n“输入呼叫者id:”
读取呼叫者id
caller\u details=$(grep-m1“$caller\u id”subscribers.txt)
调用方名称=$(echo$调用方详细信息| cut-d\|-f2)
来电者年龄=$($(日期+%Y)-$(回显$caller_详情| cut-d\|-f4)))
caller|u town=$(grep-m1“$(echo$caller|u details | cut-d\|-f5)“towns.txt | cut-d\|-f2)
printf“来电显示:%s |姓名:%s |年龄:%s |城镇:%s\n”“$Caller | ID”“$Caller | Name”“$Caller | Age”“$Caller | Town”
回声“========================================================================================================================================================================================================================================================================================================================================================================
printf“%-10s |%-8s |%-3s |%-16s |%-16s |%-16s |%s\n”“日期”“时间”“Dur”“Callee”“Caller”“Callee Town”“Caller Town”
对于$(awk-v id=“$caller_id”-F'|''$5==id'calls.txt)中的调用,执行以下操作
调用日期=$(回显“$call”| cut-d\|-f1)
调用时间=$(回显“$call”| cut-d\|-f2)
呼叫持续时间=$(回显“$call”| cut-d\|-f3)
被调用方=$(grep-m1“$(echo“$call”| cut-d\|-f4)”subscribers.txt | cut-d\|-f2)
callee|u town=$(grep-m1“$(echo“$call”| cut-d\|-f6)”towns.txt | cut-d\|-f2)
调用方| town=$(grep-m1“$(echo“$call”| cut-d\|-f7)”towns.txt | cut-d\|-f2)
printf“%s |%s |%-3s |%-16s |%-16s |%-16s |%s\n”“$call|u date”“$call|u time”“$call|u duration”“$callee”“$caller|u name”“$callee|u town”“$caller|u town”
完成
样本输出:
$/测试
输入来电显示:1000010
来电显示:1000010 |姓名:丹尼尔,R B |年龄:34 |城镇:哈博罗内
===================================================================
日期|时间| Dur | Callee | Caller | Callee Town | Caller Town
01/06/2005 | 14:58:31 | 250 |马克瓦蒂,T.M.|丹尼尔,R B |哈博罗内|托诺塔
2005年3月2日| 16:31:37 | 622 | T.C.Matshameko | Daniel,R B | Francistown | W.Mohenbo
2005年9月2日| 10:58:28 | 295 |赫索,P |丹尼尔,R B |哈博罗内|哈博罗内
03/01/2005 | 20:06:26 | 325 |特拉迪,奥格|丹尼尔,R B |康|哈博罗内
03/07/2005 | 15:18:40 | 263 |国王,W |丹尼尔,R B |诺卡宁|哈博罗内
等
您真的想在纯bash中执行此操作吗?也许可以签出。Querycsv
使您能够对CSV文件使用SQL查询。