Greping in between two different lines.

I know you could use the grep "something" -A200 flag to get all the lines past your pattern. Is there a way to get all the lines in between two patterns? The -a flag will not work since all lines in between the two patterns don't have a constant number.

There are already a lot of related posts in this forum. Please search 'print lines between patterns' in the search bar of unix.com.

One related post:

http://www.unix.com/shell-programming-scripting/88534-grep-all-lines-between-2-different-patterns.html

sed would be a good choice for this kind of problems, as

sed -n /start_pattern/,/end_pattern/p FILENAME

I would also suggest sed but in my early Linux days, one of my first scripts contained "cat foo | grep -A 1000 string1 | grep -B 1000 string2". That gives you everything between string1 and string2.

You can use Perl as well:

$
$ cat -n f6
     1  this is line 1
     2  this is line 2
     3  begin
     4  some stuff here
     5  that spans
     6  multiple lines
     7  end
     8  this is line 8
     9  this is line 9
$
$ # to print everything between "begin" and "end" both inclusive
$
$ perl -lne 'print if (/begin/../end/)' f6
begin
some stuff here
that spans
multiple lines
end
$
$ # to print everything between "begin" and "end" both exclusive
$
$ perl -lne 'BEGIN{undef $/} print $1 if (/begin\n(.*?)\nend/s)' f6
some stuff here
that spans
multiple lines
$
$

tyler_durden