sed -r 's/([^ .]+ [^ .]+) /\1\n/g' <<< "The quick fox jumped over the lazy dog"
The quick
fox jumped
over the
lazy dog
The character set [^ .]+
means one or more +
characters of any kind .
excluding the ^
whitespaces. So the capture group ([^ .]+ [^ .]+)
matches for patterns as string string
. The whole regular expression has an additional whitespace at the end ([^ .]+ [^ .]+)
(it could be included in the capture group in order to preserve it).
With sed
by using the substitute s
command we replace the matched pattern by the content of the first capture group \1
and a new line character \n
instead of the whitespace. By the flag g
we repeat the command to the end of each line. The -r
option activates the extended regular expressions.
Update - this is the actual answer:
sed -r 's/(.{8}) /\1\n/g' <<< "How do we know it is going to match the pre-defined number of characters?"
How do we
know it is
going to
match the
pre-defined
number of
characters?
In this example we capture strings with length at least 8 characters (including whitespaces) followed by a whitespace.
We can check the actual length of the output lines in a way as this:
sed -r 's/(.{8}) /\1\n/g' <<< "How do we know it is going to match the pre-defined number of characters?" \
| awk '{print length}'
9
10
8
9
11
9
11
And by the help of the answers of the question How to use printf to print a character multiple times? [awk] we can achieve the desired result.
sed -r 's/(.{8}) /\1\n/g' <<< "How do we know it is going to match the pre-defined number of characters?" \
| awk '{rest=(12 - length); printf "%s%s|\n", $0, substr(".........", 1, rest)}'
How do we...|
know it is..|
going to....|
match the...|
pre-defined.|
number of...|
characters?.|
In case you want to break the words remove the final whitespace from the above regular expression /(.{8})/
. Here is an example where the max line length will be exactly 10 characters or less, where the second sed
command will trim the whitespaces around each new line.
sed -r 's/(.{10})/\1\n/g' <<< "How do we know it is going to match the pre-defined number of characters?" \
| sed -r 's/(^ | $)//g' \
| awk '{rest=(10 - length); printf "%s%s|\n", $0, substr(".........", 1, rest)}'
How do we.|
know it is|
going to..|
match the.|
pre-define|
d number o|
f characte|
rs?.......|