gpt4 book ai didi

linux - 在 bash 中操作文本文件

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:43 27 4
gpt4 key购买 nike

我需要有关 bash 中的 ext 文件操作的帮助我有三个文件calls.txt,subscribers.txt,towns.txt the link for the files我需要帮助编写一个方法,该方法将从键盘读取 caller_town ,然后搜索已调用 caller_town 的订阅者姓名。

它应该以这种格式打印:

订阅者姓名 1

  • 调用 1
  • 调用 2
  • 调用 3

订阅者姓名 2

  • 调用 1
  • 调用 2
  • 调用 3

这是我的代码:

!/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>&-

最佳答案

我想这大致可以完成工作。尝试与您自己的代码结合,而不是仅仅复制粘贴到您的作业解决方案中。

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

关于linux - 在 bash 中操作文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22425139/

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