Sample Header Ad - 728x90

unicode safe alternative for fold

10 votes
6 answers
1916 views
I'm using fold -w 3 to split a line into multiple 3 chars long, however with the GNU implementation, it does not work for text with multi-byte characters it seems. How can I achieve the above with sed? I've come up with sed -r 's/^(.{0,3})(.*)/\1\n\2/g' however this only does a single replacement: echo "111222333444555666" | sed -r 's/^(.{0,3})(.*)/\1\n\2/g' 111 222333444555666 --- Additional examples: echo "ĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄ" | sed -r 's/^(.{0,3})(.*)/\1\n\2/g' ĄĄĄ ĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄ And fold with it's corrupting behavior: echo "ĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄ" | fold -w 3 Ą� �Ą Ą� �Ą Ą�
Asked by Chris Stryczynski (6603 rep)
Jul 7, 2018, 05:24 PM
Last activity: Mar 1, 2022, 03:54 PM