Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

using grep for find values larger/less than

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-17-2009
phil_heath phil_heath is offline
Registered User
 
Join Date: May 2009
Last Activity: 11 September 2013, 10:42 PM EDT
Posts: 95
Thanks: 8
Thanked 0 Times in 0 Posts
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:


Code:
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:


Code:
name   -2  2
name1 -2  3
name3  3  3

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

thanks
Sponsored Links
    #2  
Old Unix and Linux 11-17-2009
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 17 April 2015, 4:48 PM EDT
Location: Zürich
Posts: 8,534
Thanks: 336
Thanked 1,065 Times in 928 Posts
Horses for courses.

Why bust a gut with grep when other tools make light work of this sort of thing?
Sponsored Links
    #3  
Old Unix and Linux 11-17-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 17 April 2015, 4:11 PM EDT
Location: NM
Posts: 10,428
Thanks: 327
Thanked 857 Times in 796 Posts
example awk oneliner:

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

    #4  
Old Unix and Linux 11-18-2009
phil_heath phil_heath is offline
Registered User
 
Join Date: May 2009
Last Activity: 11 September 2013, 10:42 PM EDT
Posts: 95
Thanks: 8
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #5  
Old Unix and Linux 11-18-2009
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 17 April 2015, 11:20 PM EDT
Location: Amsterdam
Posts: 10,098
Thanks: 325
Thanked 2,649 Times in 2,370 Posts
Hi Phil, have you tried reversing the comparison operators? You can find out from Jim's solution what they look like.
Sponsored Links
    #6  
Old Unix and Linux 11-18-2009
phil_heath phil_heath is offline
Registered User
 
Join Date: May 2009
Last Activity: 11 September 2013, 10:42 PM EDT
Posts: 95
Thanks: 8
Thanked 0 Times in 0 Posts
Hey,

yeah I tried that

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


Code:
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


Code:
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 ----------


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

this seems to work i think...

Last edited by phil_heath; 11-18-2009 at 05:40 PM..
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find a file whick is consuming larger disk space in file system lokeshpashine UNIX for Dummies Questions & Answers 6 04-04-2009 02:52 AM
grep two values together. tushar_tus Shell Programming and Scripting 1 02-24-2009 05:40 AM
using a vaiable parameters and values to find and tar jjminkle Shell Programming and Scripting 1 05-02-2008 05:12 PM
grep using ASCII values sriksama UNIX for Dummies Questions & Answers 1 01-09-2008 10:26 AM
grep a list of values jolan_louve Shell Programming and Scripting 3 12-06-2006 01:18 AM



All times are GMT -4. The time now is 11:22 PM.