我想要关于bash中ext文件操作的帮助 我有三个文件calls.txt,subscribers.txt,towns.txt the link for the files 我想帮助编写一种方法,从键盘读取 caller_town ,然后搜索调用 caller_town 的订阅者姓名。
它应该以这种格式打印:
订阅者名称1
订阅者名称2
这是我的代码:
!/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)
我想要这个方法的帮助,因为它为每个订阅者打印1个呼叫而不是打印多个呼叫
if [ $caller = $callerID ]
then
echo $caller_name;
echo $date"|"$time"|"$duration"|"$callee"|"$caller"|"$calleeLoc"|"$callerLoc;
fi
}
done
exec 401>&-
答案 0 :(得分:0)
我想这将大致完成这项工作。尝试与您自己的代码结合使用,而不仅仅是复制粘贴到您的作业解决方案中。
#!/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