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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

lookup table function in awk please... 2

Status
Not open for further replies.

hokky

Technical User
Nov 9, 2006
170
AU
Hi guys,

I've got data like this,
term periode rates
2006 3M 0.85
2006 7M 0.9
2006 9M 0.7
2007 2M 0.6
2007 5M 0.5

the formula is
rates_2006 = rates(3M)*0.5 + rates(9M) --> which is
0.85*0.5 + 0.7
rates_2007 = rates(2M)*0.4 --> which is
0.6 * 0.4

How do I do in awk :
like
awk '
$1 != year { please help me fill inside



year = $1
}'

 
Hi

Hmm... And how is the year involved in the posted formulas ? For what you explained this is enough :
Code:
{
  rates[$2]=$3
}

END {
  rates_2006=rates["3M"]*0.5+rates["9M"]
  rates_2007=rates["2M"]*0.4
}
Code:
awk -f ratecalc.awk /input/file/with/rates

Feherke.
 
Read your data into an associative array, eg:
Code:
NR==FNR{rates[$1,$2]=$3;next}
when the file is exhausted you may use this:
Code:
rates_2006=rates[2006,"3M"]*0.5+rates[2006,"9M"]
rates_2007=rates[2007,"2M"]*0.4

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks.. you give me the way...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top