【发布时间】:2013-07-27 11:28:08
【问题描述】:
下面是我可以更新图书数量的代码。但我似乎无法更新我的其他信息,如书名、作者、价格等。 代码如下:
if grep -q "^$bookname:$author:" BookDB.txt
then
read -p "Update Qty to what?" newQty
sed -i "s/^\($bookname:$author:[^:]*\):[^:]*:/\1:$newQty:/" BookDB.txt
echo "Book's Qty has been updated successfully!"
else
echo "$0: BookDB.Txt: no '$title' by '$author'" >&2
fi
我的数据库是这样的:
Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50
The little Red Riding Hood:Dan Lin:40.80:20:10
Harry Potter - The Phoniex:J.K Rowling:50.00:30:20
Harry Potter - The Deathly Hollow:Dan Lin:55.00:33:790
Little Prince:The Prince:15.00:188:9
Lord of The Ring:Johnny Dept:56.80:100:38
Three Little Pig:Andrew Lim:89.10:290:189
All About Ubuntu:Ubuntu Team:76.00:55:133
Catch Me If You Can:Mary Ann:23.60:6:2
Happy Day:Mary Ann:12.99:197:101
haha:gaga:1:10:1
那么我如何更改某些字段,例如当我选择书名 Lord of the ring 作者 johnny Dept 时,我可以将书名更改为可能是国王之王?
Lord of The King:Johnny Dept:56.80:100:38
【问题讨论】:
-
您应该考虑使用适当的语言来处理此类问题。我不认为 shell 是正确的工具!例如,使用
python或perl操作csv文件非常简单易读... -
无序的 csv 文件几乎不能称为“数据库”。至少,您需要将条目按可用的顺序排列,然后才能使用“数据库”一词。