Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New to Awk. Struggling with negative number formatting

Tags:

awk

Goal: to output only data that is above 1 and below -1

or

output data that is between 1 and -1

I have the basics of awk and can print column 2 (where my data is) notice I also specified a range of 0-1

awk '/[0-1]/  {print $2}' test.dat

I am also needing to have the line number so I added NR...

awk '/[0-1]/  {print $2 NR}' test.dat

To make sure I am clear, the point is to identify which lines of the data are outside of the acceptable range, so we can ignore them in our analysis. (ie anything bigger than 1 or lower than -1 is too much of a change).

Any help you can provide would be great. I have pasted some sample data below.

http://pastebin.com/7tpBAqua

like image 232
jasonleonhard Avatar asked Dec 26 '22 15:12

jasonleonhard


1 Answers

Not sure if you want to evaluate the data in every column, or if there's a specific column you need to test. Testing a single column is simplest; testing multiple or all columns is a fairly simple repetitive extension of the pattern. Since you mention column 2 specifically, let's assume you want to print column 2 only when it is between -1 and 1:

awk -F, '($2 >= -1) && ($2 <= 1) { print $2 }'

To test for the field being greater than 1 or less than -1 instead:

awk -F, '($2 <= -1) || ($2 >= 1) { print $2 }'

Printing a different field, or the entire line instead ($0) should be fairly obvious. To examine each field, either simply repeat the entire ($2 >= -1) && ($2 <= 1) { print $2 } clause for each field you're interested in (which quickly gets verbose), or something like this (not tested):

awk -F, '{ for (i = 1; i <= NF; ++i) if (($i >= -1) && ($i <= 1)) print $i; }'
like image 159
twalberg Avatar answered Dec 31 '22 14:12

twalberg