Score:0

Extracting the values greater than a threshold in text file?

in flag

I have a data text file as follows:

data=

arht    -0.0006 0.0001  0.0147  100.0   100.0
arht    -0.0006 0.0006  0.0133  100.0   100.0
bcmd    -0.0016 0.0010  0.0168  100.0   100.0
bcmd    -0.1012 0.0010  0.0184  100.0   100.0
bcmd     0.2011 0.0024  0.0126  100.0   100.0
.
.
.

I need to extract the lines that are greater than 0.10 and -0.10 for 2, 3, and 4 columns. For the above example, 2, 3, and 4 columns in the last two lines include values greater than 0.10 and -0.10. Then, the output file should be:

output:

bcmd    -0.1012 0.0010  0.0184  100.0   100.0
bcmd     0.2011 0.0024  0.0126  100.0   100.0
elmclose avatar
cn flag
awk 'sqrt($2*$2)>=.1 || sqrt($3*$3)>=.1 || sqrt($4*$4)>=.1 {print}' file.txt replace file.txt by your data file.
deepblue avatar
in flag
Dear @elmclose, if you can answer this question, I'll accept it. Your solution works well. Thank you very much.
Score:1
cn flag

In awk, the default action when an expression evaluates to true is to print the current line. Since you need to check the absolute value of the number, ignoring the sign, an easy trick (mentioned by user elmclose in a comment) is to check that the square root of the number times itself (which will return the number as a positive integer) is greater than the threshold. So you could simply do:

$ awk 'sqrt($2*$2)>=.1 || sqrt($3*$3)>=.1 || sqrt($4*$4)>=.1' file
bcmd    -0.1012 0.0010  0.0184  100.0   100.0
bcmd     0.2011 0.0024  0.0126  100.0   100.0
Score:0
cn flag

You can also use a function within awk to check the range validity.

awk 'function fg(v) {return v<=-.1 || v>=.1 ? 1 : 0} (fg($2)+fg($3)+fg($4)){print}' mkl.txt 

or more simply:

awk 'function fg(v) {return v<=-.1 || v>=.1 ? 1 : 0} fg($2)+fg($3)+fg($4)' mkl.txt 

You can define any range limits by editing .1 and -.1

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.