Absolute value in awk doesn't work?

前端 未结 4 884
臣服心动
臣服心动 2021-01-07 19:56

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         


        
相关标签:
4条回答
  • 2021-01-07 20:00

    There is a loss of precision using sqrt($9^2). That might be a problem if you want to print the absolute value as well.

    Solution: process as text, and simply remove the leading minus sign, if present.

    This guarantees that the output matches the input exactly.

    Code:

    awk '{sub("^-", "", $9); if ($9 < 500) print $9}' inputfile
    

    Summary: to get absolute value using awk, simply remove the leading minus (-) character from a field, if present.

    0 讨论(0)
  • 2021-01-07 20:05

    For quick one-liners, I use this approach:

    awk -F'\t' 'sqrt($9*$9) < 500' > output.bam
    

    It's quick to type, but for large jobs, I'd imagine that sqrt() would impose a performance hit.

    0 讨论(0)
  • 2021-01-07 20:11

    Is this too obvious and/or not elegant ?

    awk -F'\t' '$9 < 500 && $9 > -500' > output.bam
    
    0 讨论(0)
  • 2021-01-07 20:18
    awk -F'\t' 'function abs(x){return ((x < 0.0) ? -x : x)} {if (abs($9) < 500) print $0}'
    
    0 讨论(0)
提交回复
热议问题