Similar post here: awk if statement with simple math
Below is great but now I need to subtract 20 from field $4 if it's less than 20 and if its greater than 20, field five can be set to 0.
31590,Foo,70,3.5
28327,Bar,291,14.55
25155,Baz,583,29.15
24179,Food,694,34.7
28670,Spaz,67,3.35
22190,bawk,4431,132.93
29584,alfred,142,7.1
27698,brian,379,18.95
24372,peter,22,1.1
25064,weinberger,8,.04
I had something similar to this working but there's some errors with it now.
{print $0, $4-($4>20?$4:20)}
Because field $4 could be less than 20, I'm not sure the above will actually wok as I really want 20 - $4 not $4 - 20.
This gives me $4-20: {print $0, $4-($4<20?20:$4)}
but I need 20-4 if $4 is less than 20. If $4>20 field $5 is 0.
I think what you need is
Or to make it even more straightforward
If $4 is less than 20, this returns 20 - $4 as the fifth field.
If $4 is greater than or equal to 20, this returns 0 as the fifth field.