awk to delete content before and after a matched pattern

Hello,

I have been trying to write a script where I could get awk to delete data before and after a matched pattern.

For eg
Raw data

Start
NAME = John
Age = 35
Occupation = Programmer
City = New York
Certification Completed = No
Salary = 80000
End
Start
NAME = Mary
Age = 25
Occupation = Programmer
City = New York
Certification Completed = Yes
Salary = 90000
End

So I am looking to write a script with awk, which would exclude Johns records based on the pattern Certification Completed = No

However so far am only able to know how to remove data before the pattern as shown below

 awk '/Certification Completed = No/{p=1}p' <file>

This gives

Certification Completed = No
Salary = 80000
End
Start
NAME = Mary
Age = 25
Occupation = Programmer
City = New York
Certification Completed = Yes
Salary = 90000
End

However what I am looking for is

Start
NAME = Mary
Age = 25
Occupation = Programmer
City = New York
Certification Completed = Yes
Salary = 90000
End

Basically removing all data between Start and End when the pattern is matched.

Thanks for your help in advance

Try this:

awk '!/Certification Completed = No/ { printf "%s",$0 RS }' RS="End\n" infile

The following should work with any awk:

awk '/^Start/{s=x} {s=s $0 RS} /^End/{if(s!~/Certification Completed = No/) printf "%s",s}' file

--
Note: using more than a single character for RS is a GNU awk / mawk extension..

2 Likes