Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
shell脚本显示不起作用_Shell - Fatal编程技术网

shell脚本显示不起作用

shell脚本显示不起作用,shell,Shell,下面的代码是根据用户输入显示字段,程序将抓取其他剩余字段。我希望看到的正确显示是 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:

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

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 $1  $2}'
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"

不要剪切,只需尝试使用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 ""
echo“输入标题:”
阅读标题
echo“输入作者:”
阅读作者
而read-r结果
做
标题=$(回显“$result”| cut-f1-d”:
作者=$(回显“$result”| cut-f2-d:)
价格=$(回显“$result”| cut-f3-d”:)
数量_ava=$(回显“$结果”|切割-f 4-d”:
售出数量=$(回显“$结果”|切割-f 5-d”:
如果echo“$title”| grep-iq“$title_r”&&echo“$author”| grep-iq“$author_r”;
然后
记录++
fi
完成<./BookDB.txt
echo“找到的图书数量:$record”
而read-r结果
做
标题=$(回显“$result”| cut-f1-d”:
作者=$(回显“$result”| cut-f2-d:)
价格=$(回显“$result”| cut-f3-d”:)
数量_ava=$(回显“$结果”|切割-f 4-d”:
售出数量=$(回显“$结果”|切割-f 5-d”:
如果echo“$title”| grep-iq“$title_r”&&echo“$author”| grep-iq“$author_r”;
然后
记录++
echo-e“$title,$author,“\$”$price,$qty\u ava,$qty\u salled”
fi
完成<./BookDB.txt
#echo“找到的图书数量:$record”
回声“”

使用此选项将更改文件中的“:”我希望它成为same@Ken是的,它将搜索冒号并用逗号后跟空格替换每个实例。