Question: I have the following text in my file

```
---BEGIN TEXT---
any text1
anytext2
anytext3
---END TEXT---
---BEGIN TEXT---
any text4
any text5
---END TEXT---

```

I want to remove the **2nd** text block from `"---BEGIN TEXT---"` to `"---END TEXT---"`

How I can make that with a linux command

So my file will contains only:

```
---BEGIN TEXT---
any text1
anytext2
anytext3
---END TEXT---

```

I know how to remove the 1st block with the following command:

```
sed -n '/BEGIN TEXT/,/END TEXT/{p;/PAT2/q}' file.txt

```

How I can modify my `sed` comand to remove the 2nd part and not the first part? or use another command like `awk` ?

Answer: 
Instead of `sed` you can use `awk`:

```
awk '/BEGIN TEXT/{found++} found==1{print $0}' yourfile

```

`awk` processes files line by line. So here we test to see if the current line has the `BEGIN TEXT` in it. If it does, we bump the `found` variable by 1. In the next block we print the line `print $0` if the `found` variable is equal to 1.

If the file is large and we want to stop processing after `found` is greater than 1 we can add an additional block to exit

```
awk '/BEGIN TEXT/{found++} found==1{print $0} found>1{exit 0}' yourfile

```
This might work for you (GNU sed):

```
sed -r '/---BEGIN/{:a;N;/^---END/M!ba;x;s/^/x/;/^x{2}$/{x;d};x}' file

```

Gather up the lines between `---BEGIN` and `---END` and then increment a counter in the hold space (HS). If the counter is 2 delete the collection otherwise print as normal.