Calculate ending balance & charge on overdraftw
Author Message
Calculate ending balance & charge on overdraftw

Input file:
----------- cut here
b 1200
d 200
w 400
w 800
d 200
w 500
d 700
--------- cut here
Here is a script that will add the total balance where b is opening balance
and d is debit and w is withdraw, at the same time if customer balance < 0
it will add a 15 Rand or Dollar charge

Script.awk
------------- cut here
{ if (\$1 == "b") {
balance = \$2

Quote:
}

else {
if (\$1 == "d")   {
balance = balance + \$2
Quote:
}

else {
if  (\$1 == "w"){
balance = balance - \$2}
else print \$1, "not recognised"
{
if (balance < 0 ) {
balance = balance - 15}
Quote:
}

}
}
Quote:
}

END {print balance}
------------------- cut here end

Mon, 21 Jan 2002 03:00:00 GMT
Calculate ending balance & charge on overdraftw

Quote:

>Input file:
>----------- cut here
>b 1200
>d 200
>w 400
>w 800
>d 200
>w 500
>d 700
>--------- cut here
>Here is a script that will add the total balance where b is opening balance
>and d is debit and w is withdraw, at the same time if customer balance < 0
>it will add a 15 Rand or Dollar charge

>Script.awk
>------------- cut here
>{ if (\$1 == "b") {
>balance = \$2
>}
>else {
>if (\$1 == "d")   {
>balance = balance + \$2
>}
>else {
>if  (\$1 == "w"){
>balance = balance - \$2}
>else print \$1, "not recognised"
>{
>if (balance < 0 ) {
>balance = balance - 15}
>}
>     }
>    }
>}
>END {print balance}
>------------------- cut here end

Why did you post this?

New_Script.awk
------------- cut here
/^b/ {balance = \$2; next}
/^d/ (balance += \$2; next}
/^w/ (balance -= \$2; if (balance < 0){balance -= 15}; next}
{print \$1, "not recognized}
END {print balance}
------------------- cut here end

The above charges 15 dollars for each withdrawl which results in a
negative balance, while your script would charge 15 dollars for
each withdrawl _OR_ deposit that left a negative balance after
the transaction.  I doubt that you really want to do that with
deposits, but I may be incorrect.

My code is more in the "spirit/style of awk" in that it uses pattern
matching rather that the if then else structure of other languages.
Makes it easier to read, follow, and code once you are into it, IMO.

Chuck Demas
Needham, Mass.
[posted and emailed]

--
Eat Healthy    |   _ _   | Nothing would be done at all,

Die Anyway     |    v    | That no one could find fault with it.

Mon, 21 Jan 2002 03:00:00 GMT
Calculate ending balance & charge on overdraftw

Quote:
>Input file:
>----------- cut here
>b 1200
>d 200
>w 400
>w 800
>d 200
>w 500
>d 700
>--------- cut here
>Here is a script that will add the total balance where b is opening balance
>and d is debit and w is withdraw, at the same time if customer balance < 0
>it will add a 15 Rand or Dollar charge

>Script.awk
>------------- cut here
>{ if (\$1 == "b") {
>balance = \$2
>}
>else {
>if (\$1 == "d")   {
>balance = balance + \$2
>}
>else {
>if  (\$1 == "w"){
>balance = balance - \$2}
>else print \$1, "not recognised"
>{
>if (balance < 0 ) {
>balance = balance - 15}
>}
>     }
>    }
>}
>END {print balance}
>------------------- cut here end

Use the Force Romiko! It's often easier if you use pattern{action} pairs
something like this (not tested). BTW the "next" command means "that's
all I want to do with this record, get the next record and run the
script from the top".

#!/usr/bin/gawk -f
# bank account

\$1 == "b" { # opening balance
balance = \$2
next

Quote:
}

\$1 == "d" { # deposit
balance += \$2
next

Quote:
}

\$1 == "w" { # withdrawal, penalty if overdrawn
balance -= \$2
if (balance < 0) balance -= 15
next

Quote:
}

{print \$1, "not recognised"}

END {print balance}

--
Alan Linton

Mon, 21 Jan 2002 03:00:00 GMT
Calculate ending balance & charge on overdraftw
Can it be done with similar the same code without using gawk?
Quote:

>>Input file:
>>----------- cut here
>>b 1200
>>d 200
>>w 400
>>w 800
>>d 200
>>w 500
>>d 700
>>--------- cut here
>>Here is a script that will add the total balance where b is opening
balance
>>and d is debit and w is withdraw, at the same time if customer balance < 0
>>it will add a 15 Rand or Dollar charge

>>Script.awk
>>------------- cut here
>>{ if (\$1 == "b") {
>>balance = \$2
>>}
>>else {
>>if (\$1 == "d")   {
>>balance = balance + \$2
>>}
>>else {
>>if  (\$1 == "w"){
>>balance = balance - \$2}
>>else print \$1, "not recognised"
>>{
>>if (balance < 0 ) {
>>balance = balance - 15}
>>}
>>     }
>>    }
>>}
>>END {print balance}
>>------------------- cut here end

>Use the Force Romiko! It's often easier if you use pattern{action} pairs
>something like this (not tested). BTW the "next" command means "that's
>all I want to do with this record, get the next record and run the
>script from the top".

>#!/usr/bin/gawk -f
># bank account

>\$1 == "b" { # opening balance
>  balance = \$2
>  next
>}

>\$1 == "d" { # deposit
>  balance += \$2
>  next
>}

>\$1 == "w" { # withdrawal, penalty if overdrawn
>  balance -= \$2
>  if (balance < 0) balance -= 15
>  next
>}

>{print \$1, "not recognised"}

>END {print balance}

>--
>Alan Linton

Tue, 22 Jan 2002 03:00:00 GMT
Calculate ending balance & charge on overdraftw

Quote:
>Can it be done with similar the same code without using gawk?

[snip]
>>#!/usr/bin/gawk -f
>># bank account

>>\$1 == "b" { # opening balance
>>  balance = \$2
>>  next
>>}

[snip]

I assume you are referring to the "#!/usr/bin/gawk -f" statement.

Yes, you can use your preferred awk interpreter. There is nothing gawk-
specific in this program except this first line.

If you have an awk program with this first line in a file called
awkscript on a unix system you can run it with commands like :-

nawk -f awkscript datafile

or

chmod +x awkscript
awkscript datafile

The first version treats the first line in awkscript as a comment so it
is ignored and the awk interpreter specified on the command line is
used. The second version uses the first line in awkscript to specify
which interpreter to use.

You can change gawk to awk or nawk and change /usr/bin to the path where
your awk or nawk or gawk interpreter is located.

--
Alan Linton

Tue, 22 Jan 2002 03:00:00 GMT
Calculate ending balance & charge on overdraftw

Quote:

>Yes, you can use your preferred awk interpreter. There is nothing gawk-
>specific in this program except this first line.

>If you have an awk program with this first line in a file called
>awkscript on a unix system you can run it with commands like :-

>nawk -f awkscript datafile

>or

>chmod +x awkscript
>awkscript datafile

Nitpick: This should be:

./awkscript datafile

Or else I can already hear somebody saying, "But when I do 'awkscript', it