Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 11, 2012, 05:17 PM   #1
farmerdoug
macrumors 6502a
 
Join Date: Sep 2008
popen call to grep

I'd like to get the first line after a line containing "pm EDT".
I've read the man pages for grep and googled but to no avail.
Help appreciated.

I'm using Xcode4 with OS 10.7.4

thanks.

Last edited by farmerdoug; Jun 11, 2012 at 05:24 PM.
farmerdoug is offline   0 Reply With Quote
Old Jun 11, 2012, 06:15 PM   #2
mfram
macrumors 6502a
 
Join Date: Jan 2010
Location: San Diego, CA USA
You don't need grep at all. You can do this all within your program. Open the file, search until you find the string you're looking for... then take the next line after it.

If you're using a scripting language, then I agree you would use grep. But if you're using a high-level language, then it's just slowing you down. And if you are using Objective-C, then you can use NSString convenience methods or maybe regular expressions to get the data you need.
mfram is offline   0 Reply With Quote
Old Jun 11, 2012, 06:21 PM   #3
farmerdoug
Thread Starter
macrumors 6502a
 
Join Date: Sep 2008
I have to do this with a lot of files. It means opening them; reading them into a file; then searching. grep is much cleaner.

Last edited by farmerdoug; Jun 11, 2012 at 07:29 PM.
farmerdoug is offline   0 Reply With Quote
Old Jun 11, 2012, 10:02 PM   #4
kakusan
macrumors newbie
 
Join Date: Jan 2010
I don't think you can do that with grep alone. You could use grep as part of a pipeline...

In any case if you're insistent on using popen, I'd suggest sed or awk instead of grep. Something like:

sed -n '/pm EDT/{n;p;q;}'

or

awk '/pm EDT/ { getline; print; exit }'
kakusan is offline   0 Reply With Quote
Old Jun 11, 2012, 11:14 PM   #5
Mac_Max
macrumors 6502
 
Join Date: Mar 2004
If you don't mind learning (the basics of) a programming language in the process, perl has native language level support for grep.

http://perldoc.perl.org/functions/grep.html
Mac_Max is offline   0 Reply With Quote
Old Jun 12, 2012, 08:51 AM   #6
robvas
macrumors 68000
 
Join Date: Mar 2009
Location: USA
Quote:
Originally Posted by farmerdoug View Post
I'd like to get the first line after a line containing "pm EDT".
I've read the man pages for grep and googled but to no avail.
Help appreciated.

I'm using Xcode4 with OS 10.7.4

thanks.
Use two greps -

grep -A1 'pm EDT' whateverfile

That will get you the line you're looking for, AND the line after

Then, use another grep to find lines that don't match 'pm EDT'

grep -v 'pm EDT'

Stick them all one one line with a pipe:

grep -A1 'pm EDT' whateverfile | grep -v 'pm EDT'
robvas is offline   0 Reply With Quote
Old Jun 12, 2012, 11:01 AM   #7
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
Quote:
Originally Posted by robvas View Post
Use two greps -

grep -A1 'pm EDT' whateverfile

That will get you the line you're looking for, AND the line after

Then, use another grep to find lines that don't match 'pm EDT'

grep -v 'pm EDT'

Stick them all one one line with a pipe:

grep -A1 'pm EDT' whateverfile | grep -v 'pm EDT'
Yeah that sounds way more efficient than a) opening the file yourself, b) using a language like awk to parse the file.

Last edited by jared_kipe; Jun 12, 2012 at 11:09 AM.
jared_kipe is offline   0 Reply With Quote
Old Jun 12, 2012, 01:37 PM   #8
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
grep -A1 "pm EDT" file.txt | grep -v "pm EDT"

Of course if two lines in a row have pm EDT you won't get the second line in your output. I can't write the awk on my phone, but that would be better. Set a flag when you see the pattern, output a line when it's set. Clear it if the new line doesn't contain the pattern.

-Lee
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
iPhone: Cannot pick up calls on wait, Cannot make a second call holding the first call MPriya iOS 7 3 Jul 31, 2014 04:27 PM
I got a phone call from Apple about a bug report, has anyone else received a call? adcx64 iOS 7 6 Jul 23, 2013 03:07 PM
Help with TextWrangler GREP? JacobJones Mac Applications and Mac App Store 1 Jun 13, 2013 06:40 AM
grep syntax help Big Dave Mac Programming 4 Jan 24, 2013 08:54 AM

Forum Jump

All times are GMT -5. The time now is 02:54 AM.

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

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