Solved

grep between two pattern

Posted on 2010-08-27
4
993 Views
Last Modified: 2013-12-26
Hello,

I'm looking for a solution to grep strings between two patterns, example:
Following file is to grep:

line_start
something1
something2
something3
line_stop
line_start
something4
something5
something6
line_stop
line_start
....

What I want is to grep all lines between line_start and line_stop.

Any ideas ?
0
Comment
Question by:michael_ch
  • 2
4 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 167 total points
ID: 33540180
perl -ne 'print if /line_start/../line_stop/' file
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 166 total points
ID: 33540328
... or with sed
sed -n '/line_start/,/line_stop/p' example.txt
wmp
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 33540390
And if you don't want to have line_start and line_stop in the output, we could use awk -
awk '{if ($0~"line_start") {getline; while ($0!~"line_stop") {print;getline}}}' example.txt
0
 
LVL 8

Accepted Solution

by:
allen-davis earned 167 total points
ID: 33607724
you can use egrep provided to just throw away the lines you don't want.

egrep -i "line_start|line_stop"

the command above will return anything that is not "line_stop" or "line_start"
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now