Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advanced Script Help....PLEASE!!!!

Status
Not open for further replies.

peopleperson88

Technical User
Aug 8, 2002
46
US
I'm attempting to create a database for an ATM project that I'm working on.

Here is what I have so far.
A script called nawk.test. This file is used to find the member's number and then subtract the amount given from their current balance, then publish the results.

I need to be able to 1) find the balance, 2) calculate
the new balance (old balance - withdraw amount) and 3) substitute it back into the database.

I can get #1 and #2 - but cannot get the statement to
publish the new value correctly back into the database.

Here is how I am executing the script.

nawk.test 111111 50
[script][member number][withdrawl amount]

MEM=$1
FIELD=10
old_balance=`nawk ' BEGIN { FS = ":" } /^'$1'/ { print $10 } ' data_base`
new_balance=`expr $old_balance - $2`
nawk -F: -v field=$FIELD change=$new_balance ' $1 ~ /$MEM/ {$field=change;OFS=":";print $0} ' data_base >> data_base2
echo $old_balance
echo $new_balance



Here is the database that I am working with.
File name is database.

[Member Number][Fname][MI][LName][Address][ST][Zip][Balance][Last Amount][Last Type (withdrawl, deposit, etc..)]

11111:BOB:D:JONES:9909 Hoyt Way:Westminster:CO:80021:CHECKING:5000:200:D
22222:CHARLES:A:pETERS:1800 Test Street:Carter Lake:CO:80052:CHECKING:20000:50:W
33333:MARY:X:SMITH:1234 Wadsworth Blvd:Broomfield:CO:80021:CHECKING:10000:200:W


Someone plesae help me. I have been working on this for a very long time and can NOT figure this one out.

Thanks
 
Peopleperson:

nawk -F: -v field=$FIELD -v change=$new_balance '
$1 ~ /'$MEM'/ { $field=change; print $0 } ' data_base > data_base2

Your problems:

1) Need -v before change variable
2) Need quotes around $MEM variable
3) OFS=":" is unnecesary

While your solution works, you could solve this problem in one awk script. You'd not only save one call to nawk, but a call to expr.

Also, you might consider posting questions like this in the unix scirpting or awk forums.

Regards,


Ed
 
When you say "replace" it, I assume you mean that you want the new value "in place of" the old value? I suggest you take a look at sed's find an replace feature.

-jared

ps- this sounds like a school assignment..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top