31

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?

codeforester
  • 39,467
  • 16
  • 112
  • 140
AWE
  • 4,045
  • 9
  • 33
  • 42

2 Answers2

41

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
breiti
  • 621
  • 7
  • 8
5

Try the code

awk '{s=($3==0)?"-":$3/$4; print s}'
Zombo
  • 1
  • 62
  • 391
  • 407
Jotne
  • 51
  • 1
  • 1