Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

grep between two pattern

Posted on 2010-08-27
4
Medium Priority
?
1,041 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 84

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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

688 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