1

The following expression is reasonably effective at wrapping long lines of text (for the purpose of dumping to my 128 characters wide terminal window and reading):

s~(.{104,124}) ~\1\n~g

This replaces the last space character in a “wrapping window” towards the end of the terminal window line with a newline character.

The only shortcomings that matter in practice are:

  1. Long sequences of characters with no space inbetween occurring in the “wrapping window” will prevent wrapping. While certainly a shortcoming, in practice, this is not a real problem, at least not with typical word length encountered in English text.

  2. Unnecessary wraps occur when the final word of the input line falls into the aforementioned “wrapping window” and the space is replaced by a newline in order to satisfy the expression. This can be observed fairly frequently. It is just a cosmetic nuisance, but still, it is a constant reminder of the imperfection of my pragmatic solution.

Is there an easy fix to prevent problem (2) from occurring, handling the special case of the “final word”? I'm only interested in GNU sed solutions; I know more can be done in PERL or other more heavy-weight tools.

8
  • 2
    Is fold -sw 115 heavyweight? Commented Oct 8 at 10:07
  • When doing simple (and even moderately complex) things with perl, the only differences in "heaviness" is the startup overhead. That might have been significant back in the 1990s with slow processors and slow & limited RAM, but it's negligible these days. and even back then it was only relevant when there was only a small amount of data to process (i.e. with larger jobs, the startup overhead paled in comparison to the processing workload). I suggest you stop worrying about silly things like whether a tool is "heavy-weight" or not and instead learn to use the right tool for each job. Commented Oct 8 at 10:14
  • 2
    but, as @terdon suggests, use fold (from GNU coreutils) or, better yet, par. fold isn't much better than your sed s/// operation, but par is extremely flexible and very smart about how it wraps lines. Commented Oct 8 at 10:16
  • @cas, you concluded I worry about startup time or workload, but I don't. I just want to know if there's a better way using the humble sed tool. C'est de l'art pour l'art, not a practically important question. Thanks for suggesting fold and especially par, which I was unaware of. I'm seeing fold is contained in Git for Windows, which is what I'm using; for par, I would have to install Cygwin or try building myself. Commented Oct 8 at 19:52
  • 1
    Regarding "I'm only interested in GNU sed solutions" - that'll usually lead you down a dark path trying to force the wrong tool to do something when there are better tools available. In this case fold is the standard POSIX tool to do what you apparently want to do so you might want to consider using it rather than trying to beat a solution out of sed. Commented Oct 12 at 22:14

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.