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

Calculations dependant on value of two fields 2

Status
Not open for further replies.

AnotherAlan

Technical User
Feb 10, 2006
362
GB
Hi All,

Ive written some code to calculate a mid point based on the data in four fields. I would like to include another logical check to establish if either of two fields are less than 1,000,000, and if so use 1,000,000 as the value.

i.e. if either (or both) of $7 and $10 are less than 1m then use 1m as the value(s).

So, if the value of $7 is 200,000 then substitute the value, in the calculation only, for 1,000,000.

Code:
awk -F, '{lhs=$6*$10;rhs=$7*$9;tot=$7+$10;sum=(lhs+rhs)/tot};{print $2,$6,$7,$9,$10,$(NF-3),$(NF-2),$(NF-1),$NF,sum}' myfile

Not entirely sure this can be done in one awk statement but i'm thinking that maybe I can check the values against a max variable.
Something like; max=1000000 if $10<max then lhs=$6*max e.t.c
Although I cant get the syntax correct.

All help appreciated.
Alan
 
A starting point:
($7<1000000?1000000:$7)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Fabulous, thanks very much PHV.

Ugly looking statement now but it works;

Code:
awk -F, '{lhs=$6*($10<1000000?1000000:$10);rhs=($7<1000000?1000000:$7)*$9;tot=($7<1000000?1000000:$7)+($10<1000000?1000000:$10);sum=(lhs+rhs)/tot};{print $2,$6,$7,$9,$10,$(NF-3),$(NF-2),$(NF-1),$NF,sum}'

Much appreciated, a star for your efforts.

Alan
 
Hi

Alan said:
Ugly looking statement now but it works;
Note that you can avoid repeating common code fragments by using functions :
Code:
[gray]# generic[/gray]
awk -F, '{lhs=$6*[highlight]max([/highlight]$10,1000000[highlight])[/highlight];rhs=[highlight]max([/highlight]$7,1000000[highlight])[/highlight]*$9;tot=[highlight]max([/highlight]$7,1000000[highlight])[/highlight]+[highlight]max([/highlight]$10,1000000[highlight])[/highlight];sum=(lhs+rhs)/tot};{print $2,$6,$7,$9,$10,$(NF-3),$(NF-2),$(NF-1),$NF,sum}[highlight]function max(a,b){return a>b?a:b}[/highlight]'

[gray]# specific for this case[/gray]
awk -F, '{lhs=$6*[highlight]gem([/highlight]$10[highlight])[/highlight];rhs=[highlight]gem([/highlight]$7[highlight])[/highlight]*$9;tot=[highlight]gem([/highlight]$7[highlight])[/highlight]+[highlight]gem([/highlight]$10[highlight])[/highlight];sum=(lhs+rhs)/tot};{print $2,$6,$7,$9,$10,$(NF-3),$(NF-2),$(NF-1),$NF,sum}[highlight]function gem(a){return a>1000000?a:1000000}[/highlight]'


Feherke.
 
Perhaps less ugly like this ?
Code:
awk -F, '{s10=($10<1000000?1000000:$10);s7=($7<1000000?1000000:$7);lhs=$6*s10;rhs=s7*$9;tot=s7+s10;sum=(lhs+rhs)/tot};{print $2,$6,$7,$9,$10,$(NF-3),$(NF-2),$(NF-1),$NF,sum}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Today I have learnt more AWK'ness from the Masters...Thank you Gentlemen.

Appreciate the effrots
Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top