Solved

grep between two pattern

Posted on 2010-08-27
4
1,002 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
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…
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…

856 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