Minor Calculation Error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Minor Calculation Error
# 1  
Old 07-26-2011
Minor Calculation Error

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

Code:
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.

Last edited by aLHaNz; 07-27-2011 at 12:02 AM..
# 2  
Old 07-27-2011
Try to use:
Code:
cQtyA=$(($qtyA - $sold))
cQtyS=$(($qtyS + $sold))

This is POSIX correct and all modern shells should work with these expressions. But I don't know whether it helps or not.
# 3  
Old 07-27-2011
Ahh its still the same.. The price of the record still reduce though.. Is the extraction of the data wrong?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Minor changes to script

isNumeric() { numericExpression='^+$' if ! ] then echo 1; else echo 0; fi } getColumnData() { echo `echo $1|cut -d "," -f1,2 --output-delimiter " "` } containsSegment() { if ] || ] || ] ... (2 Replies)
Discussion started by: ramyags
2 Replies

2. Shell Programming and Scripting

Filtering my major and minor values

I want to remove all rows with a minor repeating count less than 30% compared to the major repeating count from my table. The values of a col(starting col 2) can assume is A,T,G,C and N. Each row has at least 2 values and at most 4 repeating values(out of ATGC). N is considered a missing value... (12 Replies)
Discussion started by: newbie83
12 Replies

3. Shell Programming and Scripting

Error in mathematical calculation using bc

I am getting the error: Runtime error (func=(main), adr=10): Divide by zero When executing the mathematical expression: echo "scale=2; 1-(0/0)"|bc How to overcome this? (5 Replies)
Discussion started by: proactiveaditya
5 Replies

4. Shell Programming and Scripting

How to filter out major and minor?

Hi, I have line like this : proj_name/module/trunk/module_1_0 where the first "1" refers to major version and second "0" refers to minor version. any AWK or command like that so that I can filter out the major and minor ? like major= command | input line minor= command |... (4 Replies)
Discussion started by: bhaskar_m
4 Replies

5. AIX

Difference between Major and Minor in AIX

Difference between Major and Minor in AIX (5 Replies)
Discussion started by: AIXlearner
5 Replies

6. Solaris

Help with Major and minor number

Hi Does anyone know what the major and minor numbers are in Solaris? (2 Replies)
Discussion started by: wisdom
2 Replies

7. Shell Programming and Scripting

sort major.minor.release_build_x

would like to order this input based on major.minor.release AND build number Label abc_def_0.0.3_build_999 2008/08/01 'Created by me.' Label abc_def_0.0.9_build_1000 2008/08/01 'Created by me.' Label abc_def_9.0.9_build_10001 2008/08/01 'Created by me.' Label abc_def_10.9.100_build_2... (4 Replies)
Discussion started by: gurpal2000
4 Replies

8. Solaris

major & minor number

Hi Can anyone tell me what is major number and minor number in the mknod command. Also what these numbers mean. I have gone through the man pages but still I couldn't understand. Regards (3 Replies)
Discussion started by: RajaRC
3 Replies
Login or Register to Ask a Question