<p>My log file is:</p>

<pre><code> Wed Nov 12 blah blah blah blah cat1
 Wed Nov 12 blah blah blah blah
 Wed Nov 12 blah blah blah blah 
 Wed Nov 12 blah blah blah blah cat2
     more blah blah
     even more blah blah
 Wed Nov 12 blah blah blah blah cat3
 Wed Nov 12 blah blah blah blah cat4
</code></pre>

<p>I want to parse out the full multiline entries where cat is found on the first line. What's the best way to do this in <code>sed</code> and/or <code>awk</code>?</p>

<p>i.e. i want my parse to produce:</p>

<pre><code> Wed Nov 12 blah blah blah blah cat1
 Wed Nov 12 blah blah blah blah cat2
     more blah blah
     even more blah blah
 Wed Nov 12 blah blah blah blah cat3
 Wed Nov 12 blah blah blah blah cat4
</code></pre>

Assuming your log file does not contain the control characters
```
'\01'
```
and
```
'\02'
```
, and that a continued line begins with precisely four spaces, the following might work:
```
```
c1=`echo -en '\01'`
c2=`echo -en '\02'`
cat logfile | tr '\n' $c1 | sed "s/$c1    /$c2/g" | sed "s/$c1/\n/g" | grep cat | sed "s/$c2/\n    /g"
```
```
Explanation: this replaces each newline with ASCII 1 (a control character that should never appear in a log file) and each sequence "newline-space-space-space-space" with ASCII 2 (another control character).  It then re-replaces ASCII 1 with newlines, so now each sequence of multiple lines is put into one line, with the old line breaks replaced by ASCII 2.  This is grepped for cat, and then the ASCII 2's are re-replaced with the newline-space-space-space-space combination.