在使用某些事物的循环不确定时显示
Display while using loop uncertain of certain things
我有这个 while 循环,在它下面我试图得到下面列出的结果。循环的目的是遍历 BookDB.txt 并找到与标题或作者匹配的所有模式,找到后循环将打印出来,但目前我的问题是我试图在之前插入一行所有匹配模式的列表称为 "Number of Records Found: X"。
number of records found: X
Title,Author,Price,QtyAvailable,QtySold
我不确定将获取记录的行放在哪里,因为如果我将它放在循环中,它将变得重复,我试图避免重复找到的记录数行
#matching item 1
number of records found: X
Title,Author,Price,QtyAvailable,QtySold
#matching item 2
number of records found: X
Title,Author,Price,QtyAvailable,QtySold
但我不确定我应该如何修改我的代码来做到这一点。需要帮助,文件的输入是
Title:Author:Price:QtyAvailable:QtySold
function search_book
{
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
record=$(grep -io "$title" BookDB.txt | sort | uniq -c)
echo -e "$title,$author,$price,$qty_ava,$qty_sold"
fi
done < ./BookDB.txt
echo ""
echo "Number of records found: " $record | cut -f1-6 -d" "
echo ""
}
你只需要将记录行放在 while 循环之前并首先回显记录然后循环将 运行 通过程序,并显示你的匹配模式列表而没有任何重复的记录
function search_book
{
echo "Enter Title: "
read title_r
echo "Enter Author: "
read author_r
record=$(grep -io "$title_r" BookDB.txt | sort | uniq -c)
echo "Number of records found: " $record | cut -f1-6 -d" "
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
#record=$(grep -io "$title" BookDB.txt | sort | uniq -c)
echo -e "$title,$author,"$"$price,$qty_ava,$qty_sold"
fi
done < ./BookDB.txt
echo ""
#echo "Number of records found: " $record | cut -f1-6 -d" "
echo ""
main_menu
}
我有这个 while 循环,在它下面我试图得到下面列出的结果。循环的目的是遍历 BookDB.txt 并找到与标题或作者匹配的所有模式,找到后循环将打印出来,但目前我的问题是我试图在之前插入一行所有匹配模式的列表称为 "Number of Records Found: X"。
number of records found: X
Title,Author,Price,QtyAvailable,QtySold
我不确定将获取记录的行放在哪里,因为如果我将它放在循环中,它将变得重复,我试图避免重复找到的记录数行
#matching item 1
number of records found: X
Title,Author,Price,QtyAvailable,QtySold
#matching item 2
number of records found: X
Title,Author,Price,QtyAvailable,QtySold
但我不确定我应该如何修改我的代码来做到这一点。需要帮助,文件的输入是
Title:Author:Price:QtyAvailable:QtySold
function search_book
{
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
record=$(grep -io "$title" BookDB.txt | sort | uniq -c)
echo -e "$title,$author,$price,$qty_ava,$qty_sold"
fi
done < ./BookDB.txt
echo ""
echo "Number of records found: " $record | cut -f1-6 -d" "
echo ""
}
你只需要将记录行放在 while 循环之前并首先回显记录然后循环将 运行 通过程序,并显示你的匹配模式列表而没有任何重复的记录
function search_book
{
echo "Enter Title: "
read title_r
echo "Enter Author: "
read author_r
record=$(grep -io "$title_r" BookDB.txt | sort | uniq -c)
echo "Number of records found: " $record | cut -f1-6 -d" "
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
#record=$(grep -io "$title" BookDB.txt | sort | uniq -c)
echo -e "$title,$author,"$"$price,$qty_ava,$qty_sold"
fi
done < ./BookDB.txt
echo ""
#echo "Number of records found: " $record | cut -f1-6 -d" "
echo ""
main_menu
}