[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to combine lines into paragraphs?
From: |
Wolfgang Laun |
Subject: |
Re: How to combine lines into paragraphs? |
Date: |
Fri, 9 Sep 2022 20:49:31 +0200 |
$cat para.awk
BEGIN { RS = RS RS RS; del = ""; } # three RS if two blank lines separate
paragraphs
{ gsub( /\n+/, " " ); print del $0; del = "\n"; }
$cat data
line 1
line 2
line 3
line 4
line 5
line 6
$ gawk -f para.awk <data
line 1 line 2
line 3 line 4 line 5 line 6
$
This assumes that a single blank line should be rendered as a space.
It's a somewhat shaky way of separating paragraphs. What if there are 3, 4,
5,... empty lines? Is a single blank input line really just a space in the
output?
Wolfgang
On Fri, 9 Sept 2022 at 17:17, Peng Yu <pengyu.ut@gmail.com> wrote:
> Here is what I have currently. I am not sure if there is any better
> way to implement it?
>
> {
> if(/^$/) {
> if(line != "") {
> if(!printed) {
> printed = 1
> } else {
> print ""
> }
> print line
> line = ""
> }
> } else {
> if(line == "") {
> line = $0
> } else {
> line = line " " $0
> }
> }
> }
> END {
> if(line != "") {
> if(!printed) {
> printed = 1
> } else {
> print ""
> }
> print line
> }
> }
>
> On Fri, Sep 9, 2022 at 9:56 AM Peng Yu <pengyu.ut@gmail.com> wrote:
> >
> > Hi,
> >
> > ===
> > line1
> > line2
> >
> >
> > line3
> > line4
> > ===
> >
> > For example, for the above input, the output should be the following
> >
> > ===
> > line1 line2
> >
> > line3 line4
> > ===
> >
> > What is the most succinct way to do so in awk?
> >
> > --
> > Regards,
> > Peng
>
>
>
> --
> Regards,
> Peng
>
>
--
Wolfgang Laun