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

```
Never use

```
/foo/,/bar/

```

in awk unless you want to get from the first occurrence of "foo" to the last occurrence of "bar" as it makes trivial jobs marginally briefer but even slightly more interesting requirements require a complete re-write.

Just use:

```
/foo/{f=1} f{print; if (/bar/) f=0}

```

or similar instead.

In the case the awk solution is:

```
awk '/pattern/{f=1} f{print; if (!NF) exit}' file

```