AWE
AWE

Reputation: 4135

Awk if else issues

Bash points an arrow to "else" and says "syntax error" in a provocative whining tone.

awk '{if($3 != 0) a = ($3/$4) print $0, a; else if($3==0) print $0, "-" }' file > out

Why?

Upvotes: 31

Views: 168259

Answers (2)

Jotne
Jotne

Reputation: 51

Try the code

awk '{s=($3==0)?"-":$3/$4; print s}'

Upvotes: 5

breiti
breiti

Reputation: 651

You forgot braces around the if block, and a semicolon between the statements in the block.

awk '{if($3 != 0) {a = ($3/$4); print $0, a;} else if($3==0) print $0, "-" }' file > out

Upvotes: 41

Related Questions