Solved

grep between two pattern

Posted on 2010-08-27
4
995 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

911 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

20 Experts available now in Live!

Get 1:1 Help Now