Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 23, 2013, 09:24 PM   #1
Big Dave
macrumors 6502
 
Join Date: Nov 2007
Location: Crestview, Fl
grep syntax help

Let's say I have a file that contains ordered pairs of data.
Example..
-10 -10
-10 -9
...
10 9
10 10

If I want to grep out a portion of this file, say from -5 -5 to 5 5, I would use grep but I am returning extra data such as -5 6 which is outside of my search. Here is the code I have but I could use some help with my grep term.

Code:
#!/bin/bash
for ((x=-5; x<=5; x++))
  do
   for ((y=-5; y<=5; y++))
     do
      grep -- "$x $y" file
     done
  done
Has anyone seen a problem like this before?
Thanks.

Last edited by Big Dave; Jan 23, 2013 at 09:25 PM. Reason: error
Big Dave is offline   0 Reply With Quote
Old Jan 23, 2013, 09:56 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
If I wanted to do this, I'd probably try awk. Just have a rangeStarted that starts false, and for each line see if you're looking at the starting value. If so, set hasStarted to true and print. Once you've found the end you can set rangeStarted back to false or just use exit to quit.

-Lee
lee1210 is offline   0 Reply With Quote
Old Jan 24, 2013, 08:27 AM   #3
willieva
macrumors 6502
 
Join Date: Mar 2010
Or even more simply:
cat file | awk '($1 <= 5 && $1 >= -5) && ($2 <=5 && $2 >= -5){print $0}'
willieva is offline   0 Reply With Quote
Old Jan 24, 2013, 09:52 AM   #4
Big Dave
Thread Starter
macrumors 6502
 
Join Date: Nov 2007
Location: Crestview, Fl
Quote:
Originally Posted by willieva View Post
Or even more simply:
cat file | awk '($1 <= 5 && $1 >= -5) && ($2 <=5 && $2 >= -5){print $0}'
This works really well. Thank you for your help!
Big Dave is offline   0 Reply With Quote
Old Jan 24, 2013, 09:54 AM   #5
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Unfortunately, the logic is more complex. If $1 is between -4 and 4, print. If $1 is -5 and $2 is >= -5, print. If $1 is 5 and $2 is <= 5, print. Doing it based on a range seemed like less code.

-Lee

Edit: The OP said it worked... So good news. I'd expect you wouldn't get -4 8, 3 10, etc. but maybe I'm misreading.
lee1210 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Help with TextWrangler GREP? JacobJones Mac Applications and Mac App Store 1 Jun 13, 2013 07:40 AM
Syntax Coloring Gone. . . ahan.tm iPhone/iPad Programming 9 Dec 4, 2012 04:44 AM
Help with Void Syntax tulipano Mac Programming 15 Jul 11, 2012 07:27 AM
Objective C Syntax nashyo iPhone/iPad Programming 6 Jul 4, 2012 12:56 PM
popen call to grep farmerdoug Mac Programming 7 Jun 12, 2012 02:37 PM

Forum Jump

All times are GMT -5. The time now is 10:15 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC