Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute value in awk doesn't work?

Tags:

absolute

awk

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..

like image 667
madkitty Avatar asked Jun 25 '12 07:06

madkitty


People also ask

How do you do absolute value in Unix?

ABS or ABSVAL scalar function Returns the absolute value of the argument. The argument can be any built-in numeric data type. The result has the same data type and length attribute as the argument.

How do I find absolute value in Linux?

The abs() function computes the absolute value of the integer argument j. The labs(), llabs(), and imaxabs() functions compute the absolute value of the argument j of the appropriate integer type for the function.

What is the use of awk command?

The awk command is a Linux tool and programming language that allows users to process and manipulate data and produce formatted reports. The tool supports various operations for advanced text processing and facilitates expressing complex data selections.


4 Answers

awk -F'\t' 'function abs(x){return ((x < 0.0) ? -x : x)} {if (abs($9) < 500) print $0}'
like image 102
Kane Avatar answered Oct 23 '22 14:10

Kane


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.

like image 28
TheAmigo Avatar answered Oct 23 '22 14:10

TheAmigo


Is this too obvious and/or not elegant ?

awk -F'\t' '$9 < 500 && $9 > -500' > output.bam
like image 3
champost Avatar answered Oct 23 '22 13:10

champost


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.

like image 1
Roman Kogan Avatar answered Oct 23 '22 13:10

Roman Kogan