-1

I want to extract lines between two patterns (say, pattern1 and pattern2). The structure of the file is the following:

a random number of lines containing other stuff
pattern1
some lines to be extracted (finding out the number of lines possible, if necessary)
pattern2
a random number of lines containing other stuff
pattern1
some lines to be extracted
pattern2
a random number of lines containing other stuff

This repeats for a large number of times (i.e. there is a large number of matching pattern1-pattern2 pairs). I want to extract the lines between the patterns for all matches, effectively discarding the random stuff.

How can I do this?

Jotne
  • 40,548
  • 12
  • 51
  • 55

4 Answers4

2

Using awk

awk '/pattern1/,/pattern2/'
pattern1
some lines to be extracted (finding out the number of lines possible, if necessary)
pattern2
pattern1
some lines to be extracted
pattern2

Only lines between pattern

awk '/pattern2/ {f=0;next} f; /pattern1/ {f=1}'
some lines to be extracted (finding out the number of lines possible, if necessary)
some lines to be extracted
Jotne
  • 40,548
  • 12
  • 51
  • 55
2
sed -n "/pattern1/,/pattern2/ {
 /pattern1/ !{
   /pattern2/ !p
   }
 }" InputFile

printing line BETWEEN pattern, excluding the patterns itself

NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43
1

You can use sed for this:

cat inputfile | sed -ne '/pattern1/,/pattern2/p'
Mark Veenstra
  • 4,691
  • 6
  • 35
  • 66
1

Here are two more in awk:

/pattern1/ {inside_block=1}
/pattern2/ {inside_block=0}
inside_block==1 {print $0}

OR

/pattern1 { print $0; while(getline > 0) {print $0;if (/pattern2/) break }}

Neither is as elegant as the posted solution, but both might be useful depending on other requirements of the program, or the complexity of the pattern.

DavidG
  • 399
  • 2
  • 10
  • First example prints start pattern `pattern1` line and text to extract, but not stop pattern `pattern2`. Either none, or both. Second does not work, due to missing "\" after `pattern1`. – Jotne Nov 08 '13 at 12:34