0

I get error in such simple test with two variables:

$ echo test|awk '{$2 != ""? o="ABC" r="123" : o="123 r="ABC"}END{print "o:"o" r:"r}'
awk: cmd. line:1: {$2 != ""? o="ABC" r="123" : o="123 r="ABC"}END{print "o:"o" r:"r}
awk: cmd. line:1:                     ^ syntax error
awk: cmd. line:1: {$2 != ""? o="ABC" r="123" : o="123 r="ABC"}END{print "o:"o" r:"r}
awk: cmd. line:1:                                                         ^ syntax error
awk: cmd. line:1: {$2 != ""? o="ABC" r="123" : o="123 r="ABC"}END{print "o:"o" r:"r}
awk: cmd. line:1:                                                                ^ unterminated string
$

with single variables it works fine:

$ echo test|awk '{$1 != ""? o="ABC" : o="123"}END{print "o:"o}'
o:ABC
$ echo test|awk '{$2 != ""? o="ABC" : o="123"}END{print "o:"o}'
o:123
3

You can't really do that with the ? : operator, since it can handle a single value only, and two assignments would generate two values.

Use an actual if statement instead:

$ echo test|awk '{ if ($2 != "") { o="ABC"; r="123" } else { o="123"; r="ABC" }} END{ print "o:"o" r:"r}'
o:123 r:ABC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.