Question: I need to grep a file from a line containing `Pattern A` to a first empty line. 
I used `awk` but I don't know how to code this empty line.

```
cat ${file} | awk '/Pattern A/,/Pattern B/'

```

Answer: 
`sed` might be best:

```
sed -n '/PATTERN/,/^$/p' file

```

To avoid printing the empty line:

```
sed -n '/PATTERN/,/^$/{/^$/d; p}' file

```

or even better - [thanks jthill!](https://stackoverflow.com/questions/23831767/grep-awk-a-file-from-a-to-first-empty-line/23831800?noredirect=1#comment36670072_23831800):

```
sed -n '/PATTERN/,/^$/{/./p}' file

```

Above solutions will give more output than needed if `PATTERN` appears more than once. For that, it is best to quit after empty line is found, as [jaypal's answer suggests](https://stackoverflow.com/a/23831984/1983854):

```
sed -n '/PATTERN/,/^$/{/^$/q; p}' file

```

### Explanation

* `^$` matches empty lines, because `^` stands for beginning of line and `$` for end of line. So that, `^$` means: lines not containing anything in between beginning and end of line.
* `/PATTERN/,/^$/{/^$/d; p}`
	+ `/PATTERN/,/^$/` match lines from `PATTERN` to empty line.
	+ `{/^$/d; p}` remove (`d`) the lines being on `^$` format, print (`p`) the rest.
* `{/./p}` just prints those lines having at least one character.

---

With `awk` you can use:

```
awk '!NF{f=0} /PATTERN/ {f=1} f' file

```

Same as `sed`, if it has many lines with `PATTERN` it would fail. For this, let's exit once empty line is found:

```
awk 'f && !NF{exit} /PATTERN/ {f=1} f' file

```

### Explanation

* `!NF{f=0}` if there are no fields (that is, line is empty), unset the flag `f`.
* `/PATTERN/ {f=1}` if PATTERN is found, set the flag `f`.
* `f` if flag `f` is set, this is True, so it performs the default awk behaviour: print the line.

### Test

```
$ cat a
aa
bb
hello
aaaaaa
bbb

ttt

$ awk '!NF{f=0} /hello/ {f=1} f' a
hello
aaaaaa
bbb
$ sed -n '/hello/,/^$/{/./p}' a
hello
aaaaaa
bbb

```
Using `sed`: 

```
sed -n '/PATTERN/,/^$/{/^$/q;p;}' file

```

Using `regex` range, you define your range from the `PATTERN` to blank line (`/^$/`). When you encounter a blank line, you quit else you keep printing. 

Using `awk`: 

```
awk '/PATTERN/{p=1}/^$/&&p{exit}p' file 

```

You enable a flag when you encounter your `PATTERN`. When you reach a blank line and flag is enabled, you exit. If not, you keep printing. 

Another alternate suggested by [devnull](https://stackoverflow.com/questions/23831767/grep-awk-a-file-from-a-to-first-empty-line/23831984?noredirect=1#comment36672882_23831984) in the comments is to use `pcregrep`: 

```
pcregrep -M 'PATTERN(.|\n)*?(?=\n\n)' file

```