I assume that you have a shell script which calculates the value for $amt somehow.
Then in this shell script you call:
Code:
awk -v amt=$amt -v name=dan -f add.awk names.txt
Now you can use the variables name and amt in the awk script. Although you can put it on one line, it is easier to create an .awk file for it.
names.txt:
Code:
dan:13760
ricky:1235
mitch:123
add.awk
Code:
BEGIN{
FS=":"
}
{
if (name == $1)
print "was: " $1 " " $2 " becomes: "$1 " " $2+amt
else
print "was: " $1 " " $2 " remains: "$1 " " $2
}
Calling:
Code:
awk -v amt=1000 -v name=dan -f add.awk names.txt
This yields:
Code:
was: dan 13760 becomes: dan 14760
was: ricky 1235 remains: ricky 1235
was: mitch 123 remains: mitch 123
This is not exactly the output format you need, but I wrote it like this for clarity. You can easy adapt it to your preferences.
To pipe everyting into a file, you can append "> outputfile" to the awk command.
Remember there is NO way that inside and awk script you can recognize a bash variable. All values have to be passed thru the command line using the form -v var1=foo -v var2=bar.. etc.
jlinkels