gawk - using awk for subtraction -


similar post here: awk if statement simple math

below great need subtract 20 field $4 if it's less 20 , if greater 20, field 5 can set 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 similar working there's errors now.

{print $0, $4-($4>20?$4:20)}

because field $4 less 20, i'm not sure above wok want 20 - $4 not $4 - 20.

this gives me $4-20: {print $0, $4-($4<20?20:$4)} need 20-4 if $4 less 20. if $4>20 field $5 0.

i think need is

{print $0, 20 - ($4 < 20 ? $4 : 20)} 

or make more straightforward

{print $0, ($4 < 20 ? 20 - $4 : 0)} 

if $4 less 20, returns 20 - $4 fifth field.

if $4 greater or equal 20, returns 0 fifth field.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

php - Find a regex to take part of Email -

javascript - Function overwritting -