sed: Line length adjustment
7.8 Line length adjustment
==========================
This section uses 'N' and 'D' commands to search for consecutive words
spanning multiple lines, and the 'b' command for branching.
Multiline techniques and Branching and flow control.
These (somewhat contrived) examples deal with formatting and wrapping
lines of text of the following input file:
$ cat two-cities-mix.txt
It was the best of times, it was
the worst of times, it
was the age of
wisdom,
it
was
the age
of foolishness,
The following command will wrap lines at 40 characters:
$ sed -E ':x {N ; s/\n/ /g ; s/(.{40,40})/\1\n/ ; /\n/!bx ; P ; D}' \
two-cities-mix.txt
It was the best of times, it was the wor
st of times, it was the age of wisdom, i
t was the age of foolishness,
The following command will split lines by comma character:
$ sed -E ':x {N ; s/\n/ /g ; s/,/,\n/ ; /\n/!bx ; s/^ *// ; P ; D}' \
two-cities-mix.txt
It was the best of times,
it was the worst of times,
it was the age of wisdom,
it was the age of foolishness,
Both examples use similar construct:
* The ':x' is a label. It will be used later by the 'b' command to
jump to the beginning of the 'sed' program without starting a new
cycle.
* The 'N' command reads the next line from the input file, and
appends it to the existing content of the pattern space (with a
newline preceding it).
* The first 's/\n/ /g' command replaces all newlines with spaces,
discarding the line structure of the input file.
* The second 's///' command adds newlines based on the desired
pattern (after 40 characters in the first example, after comma
character in the second example).
* The '/\n/!bx' command searches for a newline in the pattern space
('/n/'), and if it is _not_ found ('!'), branches (=jumps) to the
previously defined label 'x'. This will cause 'sed' to read the
next line without processing any further commands in this cycle.
* If a newline is found in the pattern space, 'P' is used to print up
to the newline (that is - the newly structured line) then 'D'
deletes the pattern space up to the newline, and starts a new
cycle.