3

The sample input is

1
2
3


4
5



6
7
8



9

The expected output is

1
2
3
---
4
5
---
6
7
8
---
9
0
7

One way:

cat -s file | sed 's/^$/---/'

From man page of cat :

   -s, --squeeze-blank
          never more than one single blank line

Once cat has squeezed the blank lines, sed replaces the blank with with a ---

0
4

A POSIX-compliant approach using AWK's multi-line record capability (null RS):

awk 1 RS= ORS='\n---\n' file

This yields output identical to your desired output, except for a "---" line at the very end. If that discrepancy is unacceptable, it can be eliminated with a sed filter:

... | sed '$d'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.