Match pattern only between certain lines in entire file

Tags
awk, match patterns between lines, solved

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 06-20-2018
Match pattern only between certain lines in entire file

Hello, I have input that looks like this:
Code:
          * 0 -1 103 0 0 m. 7 LineNr 23 ClauseNr 1: 1: 1: 304: 0 0 SentenceNr 13 TxtType: Q Pargr: 2.1 ClType:MSyn
 PS004,006 ZBX=                0   1  1  0  7 -1 -1    3  2  3  2    -1   1   1  -1      -1      -1      -1    0  501     0
 PS004,006 ZBX                 0   2 -1 -1 -1  5 -1   -1 -1  3  2     1   2   0  -1       2      -1      -1   -1   -1    -1
 PS004,006 YDQ                 0   2 -1 -1 -1  1 -1   -1 -1  1  2     2   2   2   1  -10002      -1      -1    0  503     0
           * 0 -3 200 1 201 0 0 .. 5 LineNr 24 ClauseNr 1: 1: 2: 103: 0 0 SentenceNr 14 TxtType: Q Pargr: 2.1 ClType:ZIm0
 PS004,006 W                   0   6 -1 -1 -1 -1 -1   -1 -1 -1 -1    -1   6   6  -1      -1      -1      -1    0  509     0
 PS004,006 BVX                 0   1  1  0  7 -1 -1    3  2  3  2    -1   1   1  -1      -1      -1      -1    0  501     0
 PS004,006 >L                  0   5 -1 -1 -1 -1 -1   -1 -1 -1 -1    -1   5   0  -1      -1      -1      -1   -1   -1    -1
 PS004,006 JHWH                0   3 -1 -1 -1  1 -1   -1 -1  1  2     2   3   5   2      -1      -1      -1    0  504     0
           * 0 -1 201 0 0 .. 6 LineNr 25 ClauseNr 1: 1: 3: 153: 0 0 SentenceNr 15 TxtType: Q Pargr: 2.1 ClType:WIm0
 PS004,007 RB                  0  13 -1 -1 -1  4 -1   -1 -1  3  2     2   2   2   1      -1      -1      -1    0  502     0
 PS004,007 >MR                -1   1  0  0  1  4 -1    6  0  3  2     2   1   1  -1      -1      -1      -1    0  521     0
           * 0 -18 163 1 999 2 136 0 0 .# 2 LineNr 26 ClauseNr 1: 1: 2: 106: 0 0 SentenceNr 16 TxtType: Q Pargr: 2.2 ClType:Ptcp
 PS004,007 MJ                  0   9 -1 -1 -1 -1 -1   -1 -1 -1 -1    -1   9   9   1      -1      -1      -1    0  502     0
 PS004,007 R>H                 0   1  2  2  1 -1 -1    1  3  1  2    -1   1   1  -1      -1      -1      -1    0  501     0
 PS004,007 NW                 -1   7 -1 -1 -1 -1 -1   -1  1  3 -1    -1   7   7   2      -1      -1      -1    0  503     0
 PS004,007 VWB                 0  13 -1 -1 -1  1 -1   -1 -1  1  0     2   2   2   1      -1      -1      -1    0  503     0
           * 0 -1 999 0 0 .q 4 LineNr 27 ClauseNr 1: 1: 4: 121: 0 0 SentenceNr 17 TxtType: QQ Pargr: 2.2.1 ClType:XYqt

I would like to use either awk, sed, or grep to match a regex, but print not only line that contains the match, but also those lines before and after that match until a line that begins with a certain character.

So, for example, in the input above, if I would like to match the pattern "BVX" in field 2, I would desire the output to include not only that line, but also those between the nearest two lines before and after beginning with "*".

Thus the desired output would be:
Code:
           * 0 -3 200 1 201 0 0 .. 5 LineNr 24 ClauseNr 1: 1: 2: 103: 0 0 SentenceNr 14 TxtType: Q Pargr: 2.1 ClType:ZIm0
 PS004,006 W                   0   6 -1 -1 -1 -1 -1   -1 -1 -1 -1    -1   6   6  -1      -1      -1      -1    0  509     0
 PS004,006 BVX                 0   1  1  0  7 -1 -1    3  2  3  2    -1   1   1  -1      -1      -1      -1    0  501     0
 PS004,006 >L                  0   5 -1 -1 -1 -1 -1   -1 -1 -1 -1    -1   5   0  -1      -1      -1      -1   -1   -1    -1
 PS004,006 JHWH                0   3 -1 -1 -1  1 -1   -1 -1  1  2     2   3   5   2      -1      -1      -1    0  504     0
           * 0 -1 201 0 0 .. 6 LineNr 25 ClauseNr 1: 1: 3: 153: 0 0 SentenceNr 15 TxtType: Q Pargr: 2.1 ClType:WIm0

This is a very long file where a given pattern (such as "BVX" in the example) can occur multiple times. I would like to print each match of "BVX" and the lines before it stopping at /^\*/ and after the match stopping at /^\*/.

I have attempted combinations of grep and sed, but to no avail, e.g.
Code:
grep -C5 "BVX" input | sed -n '/\*/,/\*/p'

Thank you so much in advance.
# 2  
Old 06-20-2018
How about
Code:
awk '
                {BUF = BUF ORS $0
                }
$2 == "BVX"     {PRT = 1
                }
/^ *\*/         {if (PRT) print BUF
                 BUF = $0 
                 PRT = ""
                }
' file
           * 0 -3 200 1 201 0 0 .. 5 LineNr 24 ClauseNr 1: 1: 2: 103: 0 0 SentenceNr 14 TxtType: Q Pargr: 2.1 ClType:ZIm0
 PS004,006 W                   0   6 -1 -1 -1 -1 -1   -1 -1 -1 -1    -1   6   6  -1      -1      -1      -1    0  509     0
 PS004,006 BVX                 0   1  1  0  7 -1 -1    3  2  3  2    -1   1   1  -1      -1      -1      -1    0  501     0
 PS004,006 >L                  0   5 -1 -1 -1 -1 -1   -1 -1 -1 -1    -1   5   0  -1      -1      -1      -1   -1   -1    -1
 PS004,006 JHWH                0   3 -1 -1 -1  1 -1   -1 -1  1  2     2   3   5   2      -1      -1      -1    0  504     0
           * 0 -1 201 0 0 .. 6 LineNr 25 ClauseNr 1: 1: 3: 153: 0 0 SentenceNr 15 TxtType: Q Pargr: 2.1 ClType:WIm0


Last edited by RudiC; 06-20-2018 at 05:07 PM.. Reason: Removed surplus DL = "".
This User Gave Thanks to RudiC For This Post:
jvoot (06-20-2018)
# 3  
Old 06-20-2018
Works like a charm RudiC! Thank you so much! Now I have to go try to figure out how it works.
Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl script to fill the entire row of Excel file with color based on pattern match kshitij Shell Programming and Scripting 2 08-31-2018 06:54 PM
Match all lines in file where specific text pattern is less than cmccabe Shell Programming and Scripting 3 12-27-2016 09:06 AM
Input file needs to match a column and print the entire line lewk Shell Programming and Scripting 5 06-16-2016 06:11 PM
Removing multiple lines from input file, if multiple lines match a pattern. jxfish2 Shell Programming and Scripting 7 09-28-2015 02:58 PM
Match Pattern and print pattern and multiple lines into one line tigerhills Shell Programming and Scripting 4 01-11-2015 10:26 AM
Printing next 6 lines from of pattern match twistedpair Shell Programming and Scripting 7 12-04-2013 05:40 PM
Help with ksh-to read ip file & append lines to another file based on pattern match prashob123 Shell Programming and Scripting 6 07-17-2013 04:02 AM
Print lines that do not match the pattern sunny1234 Shell Programming and Scripting 4 03-06-2013 01:45 PM
Join the lines until next pattern match Dipalik UNIX for Dummies Questions & Answers 3 10-01-2012 09:30 AM
Need one liner to search pattern and print everything expect 6 lines from where pattern match made chidori Shell Programming and Scripting 8 03-15-2012 12:46 PM
Match pattern in a field, print pattern only instead of the entire field lucasvs UNIX for Dummies Questions & Answers 18 03-05-2012 09:07 PM
deleting lines in a file that match a pattern without opening it osbourneric Shell Programming and Scripting 1 08-28-2010 04:58 PM
retrieve lines that match a pattern fadista UNIX for Dummies Questions & Answers 2 12-10-2008 04:40 AM
Match a pattern and copy above two lines Danish Shakil Shell Programming and Scripting 16 08-01-2008 02:28 AM
How to delete lines do NOT match a pattern JumboGeng UNIX for Dummies Questions & Answers 1 09-20-2006 06:52 AM
All times are GMT -4. The time now is 11:41 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.