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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-25-2010, 01:43 PM
|
#1
|
|
LQ Newbie
Registered: Jun 2010
Posts: 10
Rep:
|
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.
|
|
|
|
06-25-2010, 01:55 PM
|
#2
|
|
Senior Member
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833
Rep: 
|
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.
|
|
|
|
06-25-2010, 03:23 PM
|
#3
|
|
Member
Registered: Sep 2004
Location: solihull.w-mids.uk
Distribution: Debian 5.0, CentOs, Solaris 8-10
Posts: 576
Rep:
|
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 (^$).
|
|
|
|
06-25-2010, 04:09 PM
|
#4
|
|
Senior Member
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833
Rep: 
|
Quote:
Originally Posted by Robhogg
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')
|
|
|
|
06-25-2010, 06:21 PM
|
#5
|
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,288
|
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.
|
|
|
|
06-26-2010, 12:22 AM
|
#6
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,384
|
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

|
|
|
|
06-28-2010, 11:44 AM
|
#7
|
|
Senior Member
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833
Rep: 
|
Quote:
Originally Posted by grail
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.
|
|
|
|
06-28-2010, 08:38 PM
|
#8
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,384
|
Sorry  wasn't trying to be smart .... just like using awk 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:50 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|