Next: , Previous: , Up: Examples   [Contents][Index]


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. See 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:


Next: , Previous: , Up: Examples   [Contents][Index]