7

I searched a bit, but didn't find a solution for this specific situation. Given a pipe that outputs groups of an arbitrary number of non-blank lines separated single blank lines, is there a sed one-liner (or awk one-liner or perl one-liner) that will combine the groups of non-blank lines into single lines, while preserving the blank lines? For example, the input

one
two

three
four
five

six

seven
eight

should be output as

one two

three four five

six

seven eight

Thanks in advance to all who respond.

4 Answers 4

8

This might work for you (GNU sed):

sed '/./{:a;N;s/\n\(.\)/ \1/;ta}' file

If the line is not empty read the following line and if that is not empty replace the newline by a space and repeat, otherwise print the pattern space. If the line was empty in the first place print the empty line: this caters for an empty first line, if this is not the case then and there is only one empty line between non-blank lines:

 sed ':a;N;s/\n\(.\)/ \1/;ta' file

is suffice.

5

Perl one-liner

perl -00 -lpe 'tr/\n/ /'

where

  • -00 reads the input in blank-line-separated paragraphs
  • -l automatically handles end-of-line newlines
  • -p automatically prints each record after processing
  • tr/\n/ /' changes all newlines to spaces
0
5

awk solution: Set RS to blank line and ORS to two new lines, if you do not wish to have blank lines in output, just remote ORS from below command.

awk -v RS= -v ORS="\n\n" '{$1=$1}1' foo.in
one two

three four five

six

seven eight
3

Sample input modified to include more than one consecutive blank lines

$ cat ip.txt 
one
two


three
four
five

six

seven
eight

awk solution:

$ awk -v RS= -v ORS="\n\n" '{gsub("\n"," "); print}' ip.txt 
one two

three four five

six

seven eight

To preserve multiple blanks as well:

$ perl -0777 -pe 's/[^\n]\K\n(?=^[^\n])/ /mgs' ip.txt 
one two


three four five

six

seven eight
  • -0777 will slurp entire file as a string, so not suitable if input file is large enough to not fit in memory
  • The regex matches non-newline character followed by newline and next line not being blank line. lookbehind and lookahead are used for easy replacement and avoiding recursive search

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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