awk calculation with zero as N/A


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk calculation with zero as N/A
# 1  
Old 05-23-2016
awk calculation with zero as N/A

In the below awk, I am trying to calculate percent for a given id. It is very close the problem is when the # being used in the calculation is zero. I am not sure how to code this condition into the awk as it happens frequently. The portion in italics was an attempt but that lead to an error. Thank you Smilie.

file1 ( $2 is used in the calculation)
Code:
ABCA2 0
ABHD12 10

file2 ( $3 is used in the calculation)
Code:
ABCA2 9 232
ABHD12 211 648 
ABL2 83 0

awk with error
Code:
awk 'function ceil(v) {return int(v)==v?v:int(v+1)}
        NR==FNR{f1[$1]=$2; next} 
       $1 in f1{print $1, ceil(10000*(1-f1[$1]/$3))/100 "%"} {print ($2 == 0 ? "NaN" : $1 / $2)}' file1 file2
awk: cmd. line:3: (FILENAME=file2 FNR=1) fatal: division by zero attempted


Last edited by RudiC; 05-23-2016 at 03:48 PM.. Reason: Added missing ICODE tag.
# 2  
Old 05-23-2016
Try something more like:
Code:
awk '
function ceil(v) {
     return int(v)==v?v:int(v+1)
}
NR==FNR{
     f1[$1]=$2
     next
} 
$1 in f1{
     print $1, ($3 == 0) ? "NaN" : (ceil(10000*(1-f1[$1]/$3))/100 "%")
}
{    print ($2 == 0 ? "NaN" : $1 / $2)
}' file1 file2

Note, however, that $1/$2 is always going to be zero when $1 is a string that starts with non-numeric characters (other than a few magic strings like Infinity and NaN).

Last edited by Don Cragun; 05-23-2016 at 04:40 PM.. Reason: Fix typo as pointed out by RavinderSingh13: s/: "Nan"/? "Nan"/
# 3  
Old 05-23-2016
Hello Don,

Thank you for nice code, just correcting a typo here.
Code:
 awk '
function ceil(v) {
     return int(v)==v?v:int(v+1)
}
NR==FNR{
     f1[$1]=$2
     next
} 
$1 in f1{
     print $1, ($3 == 0) ? "NaN" : (ceil(10000*(1-f1[$1]/$3))/100 "%")
}
{    print ($2 == 0 ? "NaN" : $1 / $2)
}' file1 file2

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 05-23-2016
You're testing if $2 == 0 - which is good! But, you don't test if $3 == 0, and I guess that's where the error occurs.
# 5  
Old 05-23-2016
Quote:
Originally Posted by RavinderSingh13
Hello Don,

Thank you for nice code, just correcting a typo here.
Code:
 awk '
function ceil(v) {
     return int(v)==v?v:int(v+1)
}
NR==FNR{
     f1[$1]=$2
     next
} 
$1 in f1{
     print $1, ($3 == 0) ? "NaN" : (ceil(10000*(1-f1[$1]/$3))/100 "%")
}
{    print ($2 == 0 ? "NaN" : $1 / $2)
}' file1 file2

Thanks,
R. Singh
Yes. Thanks. I'll correct my earlier post in a couple of minutes.

- Don
# 6  
Old 05-23-2016
Quote:
Note, however, that $1/$2 is always going to be zero when $1 is a string that starts with non-numeric characters (other than a few magic strings like Infinity and NaN ).
How would this be fixed, is there a better approach.


Code:
awk '
function ceil(v) {
     return int(v)==v?v:int(v+1)
}
NR==FNR{
     f1[$1]=$2
     next
} 
$1 in f1{
     print $1, ($3 == 0) ? "NaN" : (ceil(10000*(1-f1[$1]/$3))/100 "%")
}
{    print ($2 == 0 ? "NaN" : $1 / $2)
}' file1 file2
CHUK 3.85%
0
CHRD NaN
0

The zero's as you already thought are an issue. Thank you Smilie.

Last edited by cmccabe; 05-23-2016 at 06:21 PM.. Reason: fixed format
# 7  
Old 05-23-2016
With your sample file2 containing:
Code:
ABCA2 9 232
ABHD12 211 648 
ABL2 83 0

What output do you hope to produce when dividing ABCA2 by 9, ABHD12 by 211, or ABL2 by 83?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk split and awk calculation in the same command

I am trying to run the awk below. My question is when I split the input, then run anotherawk to perform a calculation using that splitas the input there are no issues. When I try to combine them the output is not correct, is the split not working or did I do it wrong? Thank you :). input ... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Programming

arithmetic calculation using awk

hi there again, i need to do a simple division with my data with a number of rows. i think i wanted to have a simple output like this one: col1 col2 col3 val1 val2 val1/val2 valn valm valn/valm any suggestion is very much appreciated. thanks much. (2 Replies)
Discussion started by: ida1215
2 Replies

3. Shell Programming and Scripting

awk/sed percentage calculation

Hi all i have a text file with columns delimited with , 2010-08-18,10,24,.09751,39,7,14872,26732 . . . i would to add a extra column in the end with percentage calculation of columns 5 and 8 ie (39/26732)*100 so the output must look like ... (6 Replies)
Discussion started by: posner
6 Replies

4. Shell Programming and Scripting

Calculation in Multiple files using awk

Hi All, I have some 10 files named samp1.csv, samp2.csv,... samp10.csv Each file having the same number of fields like, Count, field1, field2, field3. And a source.csv file which has three fields field1, field2, field3. Now, i want to find the total count by taking the field1,... (8 Replies)
Discussion started by: johnwilliams.sp
8 Replies

5. Shell Programming and Scripting

awk - calculation of probability density

Hi all! I have the following problem: I would like to calculate using awk a probability of appearing of a pair of numbers x and y. In other words how frequently do these numbers appear? In the case of only one integer number x ranged for example from 1 to 100 awk one liner has the form: awk... (4 Replies)
Discussion started by: jarowit
4 Replies

6. Shell Programming and Scripting

Antilog calculation in awk or sed

Dear Friends, Anybody knows how to take antilog of an value in unix. Thanks in advance Vasanth (2 Replies)
Discussion started by: vasanth.vadalur
2 Replies

7. Shell Programming and Scripting

File Size calculation with AWK

Hello Friends, Im calculating file sizes with below AWK script. I do this before some spesific files are transferred. I run the script it works but after several running it stuck with a limit of 2147483647 (2 Gbytes -1 byte) and cant exceed this. Something is wrong and I can't proceed, would... (1 Reply)
Discussion started by: EAGL€
1 Replies

8. Shell Programming and Scripting

awk calculation problem

I have a list of coordinate data, sampled below. 54555209 784672723 I want it as: 545552.09 7846727.23 Below is my script: BEGIN {FS= " "; OFS= ","} {print $1*.01,$2*.01} This is my outcome: 5.5e7 7.8e8 How do I tell awk that I want to keep all the digits instead of outputting... (1 Reply)
Discussion started by: ndnkyd
1 Replies

9. UNIX for Advanced & Expert Users

Reattemps Calculation using awk

Dear All How are you I have files which look like this : 20080406_12:43:55.779 ISC Sprint- 39 21624032999 218925866728 20080406_12:44:07.811 ISC Sprint- 20 21620241815 218927736810 20080406_12:44:00.485 ISC Sprint- 50 21621910404 218913568053... (0 Replies)
Discussion started by: zanetti321
0 Replies

10. Shell Programming and Scripting

awk calculation

Hallo all, I have a script which creates an output ... see below: root@a7germ:/tmp/pax > cat 20061117.txt 523.047 521.273 521.034 517.367 516.553 517.793 513.114 513.940 I would like to use awk to calculate the (a)total sum of the numbers (b) The average of the numbers. Please... (4 Replies)
Discussion started by: kekanap
4 Replies
Login or Register to Ask a Question