Question: I have a document:

```
column1: 23
column2: 42
kolumn3: 24
column1: 24
column2: 35
kolumn3: 46
column1: 25
column2: 42
column1: 35
column2: 45
kolumn3: 52

```

And I'd like my output to be:

```
column1: 23
column2: 42
kolumn3: 24
column1: 24
column2: 35
kolumn3: 46
column1: 35
column2: 45
kolumn3: 52

```

The output must be in `column1`, `column2`, `kolumn3` order. Remove everything that is not in this order.

Is it possible to do this in `grep`, `sed` or `awk`?

Answer: 
This will do the trick in awk, let's call it testcol.awk:

```
$1=="column1:" {l1=$0; n=2;}
$1=="column2:" && n==2 {l2=$0; n=3;}
$1=="kolumn3:" && n==3 {print l1 "\n" l2 "\n" $0; n=1}

```

Then run something like

```
awk -f testcol.awk your_input_file

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

```
sed ':a;N;/^column1:.*\ncolumn2:/!D;N;/\nkolumn3:/b;s/.*\n//;ta' file

```

Read two lines into the pattern space and if they do not match `column1:` followed by `column2:` delete the first and repeat. Otherwise, append a third line and if this matches `kolumn3:` print all three lines, if not, remove the first two lines and jump back to the beginning of the sed script.

Another solution:

```
sed -n '/\n/!N;N;/^column1:[^\n]*\ncolumn2:[^\n]*\nkolumn3:[^\n]*$/p;//!D' file

```