Division by zero attempted error during linear conversion of values between 0.25 to 1


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Division by zero attempted error during linear conversion of values between 0.25 to 1
# 1  
Old 07-24-2017
Division by zero attempted error during linear conversion of values between 0.25 to 1

I want to implement the below formula with awk oneliner
Code:
new_value = ((old_value - old_min) / (old_max - old_min) ) * (new_max - new_min) + new_min

I want to pass the value of old_min and old_min as variable. Here is what I did for this
Code:
old_min=$(awk 'BEGIN{a=100000000000}{if ($10<0+a) a=$10} END{print a}' temp11)
old_max=$(awk 'BEGIN{a=   0}{if ($10>0+a) a=$10} END{print a}' temp11)
echo $old_min
4828
echo $old_max
449064

Then I run the command, which gives the error message
Code:
awk '{ print $0, (($10 - $old_min)/($old_max - $old_min)) * ((1 - 0.25) + 0.25)}' temp11
awk: cmd. line:1: (FILENAME=temp11 FNR=1) fatal: division by zero attempted

Then I tried simply which worked fine
Code:
awk '{ print $0, (($10 - 4828)/(449064 - 4828)) * (1 - 0.25) + 0.25}' temp11

but I need to assign $old_min and $old_max dynamically as my script.sh run it inside the loop. Thanks
# 2  
Old 07-24-2017
shell variables are NOT available in awk. You need to use the -v option:
Code:
awk -vOLDMIN="$old_min" '{print OLDMIN}'

On top of vars not being available, with your syntax above you reference a field in awk, here $0 (the entire line) as old_min is undefined thus empty thus 0.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-25-2017
Thanks the Error is gone. Here is what I did:
Code:
awk -v old_min=$old_min -v old_max=$old_max '{ print $0, (($10 - old_min)/(old_max - old_min)) * ((1 - 0.25) + 0.25)}' temp11


Last edited by sammy777888; 07-25-2017 at 11:36 AM..
This User Gave Thanks to sammy777888 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Fatal division by zero attempted

Hello. I'm writing an awk script that looks at a .csv file and calculates the weighted grade based on the scores and categories in the file. I keep getting a fatal division by zero attempted error and I know what it means but I've been looking over the code for awhile and am not sure what is... (4 Replies)
Discussion started by: Eric7giants
4 Replies

2. Shell Programming and Scripting

awk - continue when encountered division error

Hello, How can I add a logic to awk to tell it to print 0 when encountering a division by zero attempted? Below is the code. Everything in the code works fine except the piece that I want to calculate read/write IO size. I take the kbr / rs and kbw / ws. There are times when the iostat data... (5 Replies)
Discussion started by: tommyd
5 Replies

3. Shell Programming and Scripting

awk fatal:division by zero attempted bypass with a condtion

Hi Friends, My input chr1 100 200 1234E-02 0.01 0.05 10 chr1 100 200 14E-11 0.11 0.50 1 chr1 100 200 134E-22 0.00 0.65 111 My command awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$13}' input | awk '{v=($5/$6); print $0"\t"v}' OFS="\t" | awk '{$8=(log($8)/log(2)); print $0}'... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

4. Shell Programming and Scripting

Awk: cmd. line:1: fatal: division by zero attempted

when i try the snippet in the console its working fine: ps awwwux | grep php-fpm | grep -v grep | grep -v master | awk '{total_mem = $6 * 1024 + total_mem; total_proc++} END{printf("%d\n", total_mem / total_proc)}' output: but when i try the bash script: #!/bin/sh # -*- sh -*- #... (3 Replies)
Discussion started by: danieloooo
3 Replies

5. Shell Programming and Scripting

awk division error - 0

input 0 0 9820373 2069 0 0 11485482 awk '{print ($1/$3) / ($4/$7)}' input error Is there any way to fix this problem ? (25 Replies)
Discussion started by: quincyjones
25 Replies

6. Shell Programming and Scripting

awk error message: division by zero attempted

Hi, I'm executing unixbench tool v4.1 on an embedded system and I'm getting these error messages: Execl Throughput 1 2 3awk: /unixbench/unixbench-4.1.0/pgms/loops.awk:38: (FILENAME=- FNR=4) fatal: division by zero attempted Pipe Throughput 1 2 3 4 5 6 7 8 9 10awk:... (3 Replies)
Discussion started by: rogelio
3 Replies

7. Shell Programming and Scripting

Division by zero error message in AWK

How can I modify my awk code to get rid of the divion by zero error message? If I run the script without an input file, it should return error message "Input file missing" but not divison by zero. Code: #!/bin/nawk -f BEGIN { if (NR == 0) {print "Input file... (4 Replies)
Discussion started by: Pauline mugisha
4 Replies

8. Shell Programming and Scripting

error "awk: (FILENAME=- FNR=23) fatal: division by zero attempted"

Hi , I have file : after i run this command : there are error can we print blank line if output error ?? thanks.. ^^ (4 Replies)
Discussion started by: justbow
4 Replies

9. Shell Programming and Scripting

division by 0 error

Hi, I am writing a script that among other things will be checking for various files on mount points. One of the conditions is that unless the server has failed over the df command will show root ( / ). If when checking the files the script comes across /, I want it to skip it, otherwise to... (2 Replies)
Discussion started by: cat55
2 Replies

10. Shell Programming and Scripting

Displaying fraction values after arithmetic division

Hi , I have the following line of code : new=$(($old/300)) But if the value is less than 300 i get the answer as "0" . How will i get the fraction values ? sars (2 Replies)
Discussion started by: sars
2 Replies
Login or Register to Ask a Question