Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract lines between two patterns from a file [duplicate]

Tags:

linux

grep

sed

awk

I need to extract particular lines between two matching patterns from a huge file.

Let's say pattern1 (unique in a file) matches a particular line # n and pattern2 (not unique in a file) matches line # m next immediate match after line # n. Then I want to extract all lines between and including line #n to #m

Sample File contents

***************************************************************************
text line # n-2
text line # n-1
********************************* Results *********************************
SUCCEEDED
...
...
some text
***************************************************************************
text line # m+1
text line # m+2
***************************************************************************

Desired Output

********************************* Results *********************************
SUCCEEDED
...
...
some text
***************************************************************************

It would be appreciated if you could help me solve this problem

like image 617
jkshah Avatar asked Oct 04 '13 09:10

jkshah


2 Answers

Using sed:

$ sed '/start_pattern_here/,/end_pattern_here/!d' inputfile

and in OP's specific case:

$ sed '/[*]* Results [*]*/,/^[*]*$/!d' inputfile
********************************* Results *********************************
SUCCEEDED
...
...
some text
***************************************************************************

Assuming that the unique pattern was *** Results *** and the non-unique one was ********.

like image 30
devnull Avatar answered Sep 27 '22 19:09

devnull


This can be an approach:

$ awk '/pattern1/ {p=1}; p; /pattern2/ {p=0}' file
********************************* Results *********************************
SUCCEEDED
...
...
some text
***************************************************************************
  • When it finds pattern1, then makes variable p=1.
  • it just prints lines when p==1. This is accomplished with the p condition. If it is true, it performs the default awk action, that is, print $0. Otherwise, it does not.
  • When it finds pattern2, then makes variable p=0. As this condition is checked after p condition, it will print the line in which pattern2 appears for the first time.

If you want an exact match of the lines:

$ awk '$0=="pattern1" {p=1}; p; $0=="pattern2" {p=0}' file

Test

$ cat a
***************************************************************************
text line # n-2
pattern1
********************************* Results *********************************
SUCCEEDED
...
...
some text
***************************************************************************
pattern2
text line # m+2
pattern2
***************************************************************************
$ awk '/pattern1/ {p=1}; p; /pattern2/ {p=0}' a
pattern1
********************************* Results *********************************
SUCCEEDED
...
...
some text
***************************************************************************
pattern2
like image 112
fedorqui 'SO stop harming' Avatar answered Sep 27 '22 19:09

fedorqui 'SO stop harming'