Question: I have been trying to come up with a sed command that will pull certain lines from blocks of text separated by a blank line in a file. The blocks of text are as below.

```
# cat test_file.txt
line 1
line 2
line 3
line 4
line 5

line 1 
line 2
line 3
line 4
line 5

line 1 
line 2
line 3
line 4
line 5

```

I am trying to pull out line 2 an 4 from each block so the output will be like below.

```
line 2
line 4

line 2
line 4

line 2 
line 4

```

I came up with a way to do it for the first block of text using sed:

```
# sed -n -e 2p -e 4p test_flie.txt
line 2
line 4

```

But haven't been able to find a way to get it to continue for each block of text till the end of the file. Any pointers would be greatly appreciated.

Answer: 
awks paragraph mode exists specifically to handle blank-line separated records/blocks of text like you're dealing with:

```
$ awk 'BEGIN{RS=""; ORS="\n\n"; FS=OFS="\n"} {print $2, $4}' file
line 2
line 4

line 2
line 4

line 2
line 4

```

Reference [the POSIX standard](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html):

> 
> If RS is null, then records are separated by sequences consisting of a `<newline>` plus one or more blank lines, leading or trailing blank lines shall not result in empty records at the beginning or end of the input
> 
> 
> 

If you need to not have a blank line printed after the final record:

```
$ awk 'BEGIN{RS=""; FS=OFS="\n"} NR>1{print prev ORS} {prev=$2 OFS $4} END{print prev}' file
line 2
line 4

line 2
line 4

line 2
line 4

```

or if you don't want to use paragraph mode for some reason then:

```
$ awk 'BEGIN{tgts[2]; tgts[4]} !NF{print ""; lineNr=0; next} ++lineNr in tgts' file
line 2
line 4

line 2
line 4

line 2
line 4

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

```
sed -n '/\S/{n;p;n;n;p;:a;n;//ba;p}' file

```

Set the `-n` option for explicit printing. Print the second and fourth lines then throw away any non-blank lines and print the first blank one. Repeat.