gawk - Confusion about conditional expression in AWK -


here input file:

$ cat abc 0   1 2   3 4   5 

why following give one-column output instead of two-column one?

$ cat abc | awk '{ print $1==0?"000":"111" $1==0? "222":"333" }' 000 333 333 

shouldn't output following?

000 222 111 333 111 333 

i think awk going parse as:

awk '{ print ($1==0) ? "000" : (("111" $1==0) ? "222" : "333") }' 

that is, when prints 3 zeros, doesn't consider rest of operation. , when doesn't print 3 zeros, prints triple threes because "111" concatenated string not going evaluate zero.

you want use:

awk '{ print ($1==0?"000":"111"), ($1==0? "222":"333") }' 

where comma puts space (ofs or output field separator, precise) in output between 2 strings. or might prefer:

awk '{ print ($1==0?"000":"111") ($1==0? "222":"333") }' 

which concatenates 2 strings no space.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -