shell 脚本显示不工作

shell script display not functioning

我在下面得到这段代码,它根据用户输入显示字段,程序将抓取其他剩余字段,我希望看到的正确显示是

Found 2 records
The Hunger Games, Suzanne Collins, 1, 1, 1
The Hunger Games, Test, 1, 1, 1

但显示下方的当前代码是

Found 2
the hunger games
the hunger games, suzanne collins
test, 1
1, 1
1, 1
1

文件的输入是

The Hunger Games:Suzanne Collins:1:1:1
The Hunger Games:test:1:1:1

希望有人能在这方面帮助我

echo "Enter Title: "
read title
echo "Enter Author: "
read author
result=$(grep -ise "$title\:$author" BookDB.txt)
record=$(grep -io "$title" BookDB.txt | uniq -c)
if [ "$result" != "" ] && [ "$author" == "" ]
then 
echo "found: " $record "records" | awk '{print   }'
echo "" 
title=$(echo "$result" | cut -f 1 -d ":")
author=$(echo "$result" | cut -f 2 -d ":")
price=$(echo "$result" | cut -f 3 -d ":")
qty_ava=$(echo "$result" | cut -f 4 -d ":")
qty_sold=$(echo "$result" | cut -f 5 -d ":")
echo ""
echo -e "$title", "$author", ""$"$price", "$qty_ava", "$qty_sold"

不使用 cut,而是尝试像这样使用 sed:

sed 's/:/, /g' file.txt

它将搜索“:”并将其每次出现替换为“,”

好吧,我的方法并不是最好的,但是 运行 记录的双 while 循环,而不是先打印所有详细信息来计算记录,然后再次循环以获取详细信息。这不是最好或有效的方法

echo "Enter Title: "
read title_r
echo "Enter Author: "
read author_r
while read -r result
do
title=$(echo "$result" | cut -f 1 -d ":")
author=$(echo "$result" | cut -f 2 -d ":")
price=$(echo "$result" | cut -f 3 -d ":")
qty_ava=$(echo "$result" | cut -f 4 -d":")
qty_sold=$(echo "$result" | cut -f 5 -d ":")
if echo "$title" | grep -iq "$title_r" && echo "$author" | grep -iq "$author_r";
then
let record++

fi  
done < ./BookDB.txt

echo "Number of books found: " $record  
while read -r result
do
title=$(echo "$result" | cut -f 1 -d ":")
author=$(echo "$result" | cut -f 2 -d ":")
price=$(echo "$result" | cut -f 3 -d ":")
qty_ava=$(echo "$result" | cut -f 4 -d":")
qty_sold=$(echo "$result" | cut -f 5 -d ":")
if echo "$title" | grep -iq "$title_r" && echo "$author" | grep -iq "$author_r";
then
let record++
echo -e "$title,$author,"$"$price,$qty_ava,$qty_sold"      
fi  
done < ./BookDB.txt
#echo "Number of books found: " $record 
echo ""