Issue
How can sed
split a line on certain characters and with additional condition,
that is after range 30 - 35 characters.
Within a start-end marked block, that's ``` but for start mark followed by word
Characters is . , space
if it's space, replaced with newline control character on which otherwise appended
e.g.
```bash
hello world hello world hello world hello foo, bar, world hello foo, bar, world
```
to become
```bash
hello world hello world hello world
hello foo, bar, world hello foo, bar,
world
```
Farthest i could:
sed -E /^```\w/,/^```/{:bloc n; :split s/^.{30,35}[.,[:space:]]/&/;T bloc; p;s/^.{30,35}[.,[:space:]](.*)/\1\n/; b split}' -- FILE
which surely far from correct so please sincerely help show the correct path.
Solution
This might work for you (GNU sed):
sed -E '/```/{n;:a;N;//!ba;s/([^\n]{30,35}[^,. ]*[,.]*) /\1\n/g}' file
Populate the pattern space with the required stanza, then globally replace the first space within the required range with a newline.
A variation on the solution would split on the first ,
,.
or
and still absorb a following space too:
sed -E '/```/{n;:a;N;//!ba;s/([^\n]{30,35}[^,. ]*)(([,.]) ?| )/\1\3\n/g}' file
Answered By - potong Answer Checked By - Katrina (WPSolving Volunteer)