using grep for find values larger/less than

Hi

I have been experimenting with grep to find values for a particular column that is greater than or less than certain #'s.

So my file looks like this:

name   -2  2
name1 -2  2
name2 -1  4
name3  3  3

So I want to find rows with values less than or equal to -2 and those greater than or equal to 3 (for column2 only)

Then the output would look like this:

name   -2  2
name1 -2  3
name3  3  3

I tried grep but I am very lost with it..

thanks

Horses for courses.

Why bust a gut with grep when other tools make light work of this sort of thing?

example awk oneliner:

awk ' $2<=-2 || $2>=3' myfile

hi thank you for the solution. It works..

what if I want to find something within a range lets say between -2 and 3. Obviously the directions of the arrows wont work.

thanks

Hi Phil, have you tried reversing the comparison operators? You can find out from Jim's solution what they look like.

Hey,

yeah I tried that

but that will give everything greater than -3 and everything less than 2.

5
4
3
2
1
0
-1
-2
-3
-4
-5

So lets I wanted the range of values to be -3 to 2 then the values being pulled out would be

2
1
0
-1
-2
-3

but instead if I only reversed teh arrows, I get everything greater than -3 and everything less than 2

---------- Post updated at 05:32 PM ---------- Previous update was at 05:14 PM ----------

awk ' $2>=-2 && $2<=3'

this seems to work i think...