Absolute value in awk doesn't work?

madkitty picture madkitty · Jun 25, 2012 · Viewed 30k times · Source

I want to select line of a file where the absolute value of column 9 is less than 500. Column is sometimes positive, sometimes negative.

awk -F'\t' '{ if ($9 < |500|) {print $0} }' > output.bam

This doesn't work so far .. one round on internet told me that to use the absolute value we should add

func abs(x) { return (x<0) ? x*-1 : x }

Then how am I suppose to put this along with the value of column 9?? I don't know what could be a proper syntax..

Answer

Kane picture Kane · Jun 25, 2012
awk -F'\t' 'function abs(x){return ((x < 0.0) ? -x : x)} {if (abs($9) < 500) print $0}'