Reputation: 610
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.
Upvotes: 7
Views: 1862
Reputation: 23687
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 memoryUpvotes: 3
Reputation: 18411
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
Upvotes: 6
Reputation: 58528
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.
Upvotes: 8
Reputation: 247092
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 processingtr/\n/ /'
changes all newlines to spacesUpvotes: 5