LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Grep until certain character or pattern appears (http://www.linuxquestions.org/questions/programming-9/grep-until-certain-character-or-pattern-appears-816393/)

ohijames 06-25-2010 01:43 PM

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.

rweaver 06-25-2010 01:55 PM

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.

Robhogg 06-25-2010 03:23 PM

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 (^$).

rweaver 06-25-2010 04:09 PM

Quote:

Originally Posted by Robhogg (Post 4015108)
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')

syg00 06-25-2010 06:21 PM

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.

grail 06-26-2010 12:22 AM

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

rweaver 06-28-2010 11:44 AM

Quote:

Originally Posted by grail (Post 4015374)
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.

grail 06-28-2010 08:38 PM

Sorry :( wasn't trying to be smart .... just like using awk :)


All times are GMT -5. The time now is 12:17 PM.