Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

grep between two pattern

Posted on 2010-08-27
4
Medium Priority
?
1,047 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 85

Assisted Solution

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

Assisted Solution

by:woolmilkporc
woolmilkporc earned 664 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 668 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

916 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