help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to combine lines into paragraphs?


From: Miriam English
Subject: Re: How to combine lines into paragraphs?
Date: Wed, 14 Sep 2022 02:43:56 +1000

I went through a few versions using various languages, most
unsatisfactory in some way. My version using awk seems to work fine. I
can't remember why I stopped using it.

awk '{RS="" ; gsub(/\n[ \t]*/, " "); print $0 "\n"}'

In the end I opted to use sed, which works exactly how I wanted...
though, in my notes to myself I cautioned that sed can mess with
unicode characters, however I've just now done a few experiments and it
doesn't seem to.

sed -z -r 's/[ \t]*\n[ \t]*/\n/g; s/([^\n])\n([^\n])/\1 \2/g'


As a writer, I do a lot of text editing so needed this to be readily
available all the time. I put an AppRun shell script inside an AppDir
on my desktop so I can highlight some text with the mouse and click on
the AppDir. That takes the selected text, processes it, and replaces it
over the selection without needing to open the commandline.

For that simple GUI, I use xclip to pipe the selected text into what
I'm using to process the text, then pipe the processed text back out
to the clipboard using xclip again, and use xdotool to replace the
selected text.

The nice thing about this is that it works everywhere -- in text
editors, on web comment textareas, in emails, in file save dialogs...
almost everywhere. I use lots of these to change case, switch between
spaces and underscores, insert commonly needed text, insert the current
date or time, surround text with quotes or brackets, or comment
symbols, and much more.

xclip -o | ***process the text here*** | xclip  -selection c
xdotool key ctrl+v

Hope this helps,

 - Miriam


On Fri, 9 Sep 2022 09:56:27 -0500
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?
> 



-- 
There are two wolves and they're always fighting.
One is darkness and despair. The other is light and hope.
Which wolf wins?
Whichever one you feed.
  -- Casey in Brad Bird's movie "Tomorrowland"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]