# awk to delete content before and after a matched pattern

**URL:** <https://community.unix.com/t/awk-to-delete-content-before-and-after-a-matched-pattern/366000>\
**Category:** Shell Programming and Scripting\
**Created:** [April 6, 2017, 8:25pm UTC](https://community.unix.com/t/awk-to-delete-content-before-and-after-a-matched-pattern/366000 "2017-04-06T20:25:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sidnow](https://community.unix.com/letter_avatar/sidnow/32/6_449c533f586939b6e5f246632a46421f.png) [@sidnow](https://community.unix.com/u/sidnow)\
**Post date:** [April 6, 2017, 8:25pm UTC](https://community.unix.com/t/awk-to-delete-content-before-and-after-a-matched-pattern/366000/1 "2017-04-06T20:25:41Z")

</div>

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

```nohighlight
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

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

```

This gives

```nohighlight
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

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [April 6, 2017, 8:52pm UTC](https://community.unix.com/t/awk-to-delete-content-before-and-after-a-matched-pattern/366000/2 "2017-04-06T20:52:08Z")

</div>

Try this:

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

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [April 7, 2017, 2:54am UTC](https://community.unix.com/t/awk-to-delete-content-before-and-after-a-matched-pattern/366000/3 "2017-04-07T02:54:53Z")

</div>

The following should work with any awk:

```nohighlight
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..
