Reputation: 137
I have a text file.
Info in text file is
Book1:Author1:10.50:50:5
Book2:Author2:4.50:30:10
First one is book name, second is author name, third is the price, fourth is the quantity and fifth is the quantity sold.
I was trying to update the price and it works fine with:
read -p $'New Price: ' newPrice
sed "s/${Title}:${Author}:[^:]\+/${Title}:${Author}:${newPrice}/g" BookDB.txt > tempBook.txt
mv -f tempBook.txt BookDB.txt
echo "Book price updated!"
When I quit the program and add codes to update the quantity using:
read -p $'New Quantity: ' newQty
sed "s/${Title}:${Author}:${Price}:[^:]\+/${Title}:${Author}:${Price}:${newQty}/g" BookDB.txt > tempBook.txt
mv -f tempBook.txt BookDB.txt
echo "Book quantity updated!"
The quantity could not be updated simply because I kind of lost the price info. I was able to edit the quantity if I added a new book into the program right away where the price was stored, but for other books, it was impossible to edit the quantity avaliable.
Can someone please help? I was wondering if I could extract/store the value of the price of the book.
Upvotes: 2
Views: 873
Reputation: 5685
For update quantity you can use:
read -p $'New Quantity: ' newQty
sed -re "s/${Title}:${Author}:([^:]+):[^:]+/${Title}:${Author}:\1:${newQty}/g" BookDB.txt > tempBook.txt
mv -f tempBook.txt BookDB.txt
echo "Book quantity updated!"
Upvotes: 1