astha
astha

Reputation: 613

How to overcome "(FILENAME=- FNR=1) fatal: division by zero attempted" error in awk

I have a 10 column text file and I need to do some mathematical processing on it. For example, when I issue below command

cat case.dat | awk '{print ($1-0.777472), ($1*$2*$3*$4)/($10)}'

Then I get below error

awk: cmd. line:1: (FILENAME=- FNR=1) fatal: division by zero attempted

and the script does not give any output.

That means I have some zero's in $8 and thus I am getting error. How I can overcome this issue with a simple awk command?

Upvotes: 0

Views: 4971

Answers (1)

anubhava
anubhava

Reputation: 784958

Converting my comment to answer so that solution is easy to find for future visitors.

In order to avoid fatal: division by zero error have a check if $10 is non-zero before attempting to divide:

awk '{print ($1-0.777472), ($10 ? ($1*$2*$3*$4)/$10 : 0)}' case.dat

Upvotes: 3

Related Questions