0

The following awk line will fail when c15 is not defined or is 0. How to modify it so that in such cases it does not attempt the calculation, but instead provides an error message or does nothing:

cat formatted.dsv | awk -F"\t" '{ c12 = $12; c15 = $15; c17 = $17; c16 = $16; c4 = $4; c9 = $9; c5 = $5; c10 = $10; c14 = $14; c8 = $8; c7 = $7 } END { printf "%d\n", ((c12+c17+c16+c4+c9+c5+c10+c14+c8+c7))/c15)*1000 }'

Michael Martinez
  • 2,645
  • 3
  • 24
  • 35

1 Answers1

1

if a variable is undefined in awk(1) then it has a default value of null string or zero, depending on the context. I think you can use this as follows:

if (c15) printf "%d\n", ((c12+c17+c16+c4+c9+c5+c10+c14+c8+c7))/c15)*1000
else print "C15: null or empty\n"
dawud
  • 15,096
  • 3
  • 42
  • 61
Phil
  • 21
  • 1