Awk : Select only row with digits in a column and multiply those for 1000

Viewed 139

AIM

I would like to multiply column $3 rows-digits (0.16 and -0.45) for 1000! This avoiding to multiply the N/A for 0, producing a 0 where the N/A was!

Command lines I am trying to produce!

awk -F"\t" '{OFS="\t"}{print $1, $2, $3~/^[0-9]{3}$*1000/}' > out-put.txt
awk -F"\t" '{OFS="\t"}{print $1, $2, $3~/^%[0-9]%/$*1000}' > out-put.txt

FILE

I have a file with 3 columns :

abc  cat   0.16
lmm  fil   N/A
lmd  ful  -0.45
....continue

Out-put searched

abc  cat   160
lmm  fil   N/A
lmd  ful  -450
....continue
3 Answers

With your shown samples, please try following awk code. Use ternary operator to check if 3rd field is NOT N/A then multiply 1000 to it else leave it as it is.

awk '{$3=$3!="N/A"?$3 * 1000:$3} 1' Input_file

OR In case your Input_file is tab delimited then try following:

awk 'BEGIN{FS=OFS="\t"}{$3=$3!="N/A"?$3 * 1000:$3} 1' Input_file

Here is an awk solution that checks whether $3 is numeric before multiplying it with 1000:

awk 'BEGIN{FS=OFS="\t"} $3 == $3+0 {$3 *= 1000} 1' file

abc cat 160
lmm fil N/A
lmd ful -450

You can try this awk

awk '$3!="N/A"{$3*=1000}1' $file | column -t

Output

abc  cat  160
lmm  fil  N/A
lmd  ful  -450
Related