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: 
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

```
I think this is a nice, readable Perl one-liner:

```
perl -wne '$f=1 if /Pattern A/; exit if /^\s*$/; print if $f' file

```

* Set the flag `$f` when the pattern is matched
* Exit if a blank line (only whitespace between start and end of line) is found
* Print the line if the flag is set

Testing it out:

```
$ cat file
1
2
Pattern A
3
4
5
6

7
8
9

$ perl -wne '$f=1 if /Pattern A/; exit if /^$/; print if $f' file
Pattern A
3
4
5
6

```

Alternatively, based on the [suggestion by @jaypal](https://stackoverflow.com/questions/23831767/grep-awk-a-file-from-a-to-first-empty-line#comment36670533_23832395), you could do this:

```
perl -lne '/Pattern A/ .. 1 and !/^$/ ? print : exit' file

```

Rather than using a flag `$f`, the range operator `..` takes care of this for you. It evaluates to true when "Pattern A" is found on the line and remains true indefinitely. When it is true, the other part will be evaluated and will `print` until a blank line is found.