LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-25-2010, 01:43 PM   #1
ohijames
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Rep: Reputation: 0
Grep until certain character or pattern appears


Hi,

I have the following command that greps "/etc/cron.allow" and displays the following 9 lines of $file
grep -A 9 "/etc/cron.allow" $file

On the other hand I would like to grep a file for a certain text display the next couple of lines and stop when i hit a specified word or blank or pattern.

Basically I would like my grep to end when the shell hits a blank, certain key word or pattern specified in command.

Thanks.
 
Old 06-25-2010, 01:55 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
add another grep:

Code:
grep -A9 "/etc/cron.allow" $file | grep -B9 "pattern"
so what happens is...

Code:
core:~$ grep -A9 "/etc/cron.allow" test.txt
cron.allow
asdf
asdf

asdf
dfda
asdf
asdf
aasdfffds
core:~$ grep -A9 "/etc/cron.allow" test.txt | grep -B9 "^$"
cron.allow
asdf
asdf

core:~$
Since grep only passes the data you specified to the pipe and it can't pass more lines than that, the second grep limits it back to beginning of whatever matched at most from the point it occurs.

Might be more applicable to output the grep and clean it up though if your input is complex.

Last edited by rweaver; 06-25-2010 at 01:58 PM.
 
Old 06-25-2010, 03:23 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
An alternative way to do this could be using sed:

Code:
sed -n '/cron.allow/,/\<word\>\|^$\|pattern/p' $file
The -n switch turns off sed's normal behaviour (to print every line of $file), while the command in single quotes tells it to print everything between line(s) containing 'cron.allow' and the next line either containing ' word ', or matching 'pattern', or being blank (^$).
 
Old 06-25-2010, 04:09 PM   #4
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
Quote:
Originally Posted by Robhogg View Post
An alternative way to do this could be using sed:
Code:
sed -n '/cron.allow/,/\<word\>\|^$\|pattern/p' $file
I agree with Rob's use of sed here (or awk as an alternative), sed is better suited to this particular application than grep. An added advantage if you learn a little sed you don't ever need grep and sed is considerably more powerful (awk even more so than sed, although the syntax is typically a bit more complex for 'easy things')
 
Old 06-25-2010, 06:21 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,269

Rep: Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028
Simply a matter of using the "right" tool for the job.
There is no way I could imagine never finding a use for grep again.
 
Old 06-26-2010, 12:22 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,503

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
Quote:
awk even more so than sed, although the syntax is typically a bit more complex for 'easy things'
You mean like:
Code:
awk '/cron\.allow/,/pattern|^$/' file
 
Old 06-28-2010, 11:44 AM   #7
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
Quote:
Originally Posted by grail View Post
You mean like:
Code:
awk '/cron\.allow/,/pattern|^$/' file
I said typically There are some exceptions as with everything in life, very few things are black or white.
 
Old 06-28-2010, 08:38 PM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,503

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
Sorry wasn't trying to be smart .... just like using awk
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 08:05 AM
grep till second pattern rattlesnakejoe Programming 1 11-22-2009 08:09 AM
grep exact pattern kapila Linux - Newbie 11 08-06-2009 02:58 AM
using grep when the pattern contains a ! farmerjoe Programming 9 03-15-2005 11:04 PM
pattern file with no return character ksun Linux - Newbie 1 12-28-2004 06:40 PM


All times are GMT -5. The time now is 10:51 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration