Hello everyone!! I got a slight problem doing some calculation from the text file. I able to get the specific data by cutting it using grep and cut.
The amount can be calculated but the problem I faced now is even the field which I didnt cut is been calculated too.
This is what I meant.
The original
Record: Name: Price: QtyAvailable: QtySold:
SampleA A 10 10 2
SampleB B 20 30 10
User input qtySold :2 for sample A(The correct way)
Record: Name: Price: QtyAvailable: QtySold:
SampleA A 10 8 2
SampleB B 20 30 10
Problem facing now.. Notice the price is also minus.
Record: Name: Price: QtyAvailable: QtySold:
SampleA A 8 8 4
SampleB B 20 30 10
Text file(file.txt)
SampleA:A:10:10:2
SampleB:B:20:30:10
echo -n "Record: "
read rec
echo -n "Author: "
read author
if grep -q "$rec:$author" "file.txt"; then
echo -n "No. of copies sold: "
read sold
qtyA=`grep -w "$rec:$author" file.txt | cut -d: -f4`
qtyS=`grep -w "$rec:$author" file.txt | cut -d: -f5`
echo "Current Record info:"
awk -F: -vOFS=", " -vt="$rec:$author" '$0~t{$3="$"$3;print}' file.txt
echo "New Record info:"
cQtyA=`expr $qtyA - $sold`
cQtyS=`expr $qtyS + $sold`
sed "s/$qtyA/$cQtyA/g" file.txt > file1.txt && mv file1.txt file.txt
sed "s/$qtyS/$cQtyS/g" file.txt > file1.txt && mv file1.txt file.txt
awk -F: -vOFS=", " -vt="$rec:$author" '$0~t{$3="$"$3;print}' file.txt
else
echo "Record not found!"
fi
Seems that I correctly cut the specific field yet I can't calculate properly. Haha sorry for the trouble.