bayesianpower
bayesianpower

Reputation: 91

Prevent awk from adding non-integers?

I have a file that has these columns that I would like to add:

absolute_broad_major_cn
1
1
1
1
1.76
1.76
NA
1

and

absolute_broad_minor_cn
1
1
1
1
0.92
0.92
NA
1

I did awk '{ print $1+$2 }, which worked well but it put 0 for where there was an NA. Is it possible to make awk forget this and just put NA again instead (so awk only adds numbers)?

Edit: Desired output is:

<Column header> 
2
2
2
2
2.68
2.68
NA
2

Upvotes: 0

Views: 180

Answers (4)

Ed Morton
Ed Morton

Reputation: 204045

I think what you're really trying to do is sum 2 numeric columns from 1 file:

awk '{print ($1==($1+0) ? $1+$2 : $1)}' file

$1 == $1+0 will only be true if $1 is a number.

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133640

Could you please try following, written and tested with shown samples.

awk '
FNR==NR{
  a[FNR]=$0
  next
}
{
  print ($0~/[a-zA-Z]/ && a[FNR]~/[a-zA-Z]/?"NA":a[FNR]+$0)
}
' absolute_broad_major_cn  absolute_broad_minor_cn

Explanation: Adding detailed explanation for above.

awk '                                                             ##Starting awk program from here.
FNR==NR{                                                          ##Checking condition FNR==NR which will be TRUE when Input_file absolute_broad_major_cn is being read.
  a[FNR]=$0                                                       ##Creating array a with index FNR and having value as current line here.
  next                                                            ##next will skip all further statements from here.
}
{
  print ($0~/[a-zA-Z]/ && a[FNR]~/[a-zA-Z]/?"NA":a[FNR]+$0)       ##Printing either addition of current line with array a value or print NA in case any alphabate is found either in array value OR in current line.
}
' absolute_broad_major_cn  absolute_broad_minor_cn                ##Mentioning Input_file names here.

Upvotes: 2

T Wellick
T Wellick

Reputation: 11

Just remove the lines with NA & then add them awk '$1 != "NA"' FS=' ' file | awk '{ print $1+$2 }'

Upvotes: 0

mevets
mevets

Reputation: 10449

paste absolute* | awk '{ if ($1 == "NA" && $2 == "NA") print "NA"; else print $1 + $2; }'

would do the trick; whether you want && (both are "NA" to produce an "NA") or || (either one is "NA" produces an NA) is specific to your need.

Upvotes: 3

Related Questions