ebvogt
ebvogt

Reputation: 43

Split text into equal parts

I have a phrase in one field in a file, which i need to break down into parts of 30 characters (including letters, numbers and spaces).

Ex.: "

This text is to show an example of what im looking for, to break the text into parts containing maximum 30 characters."

I need the following result:

This text is to show an exampl|e of what im looking for, to b|reak the text into parts conta|ining maximum 30 characters.

Could someone point me a direction?

Upvotes: 1

Views: 916

Answers (1)

karakfa
karakfa

Reputation: 67467

This is a job for fold

$ fold -w30 longline | tr '\n' '|' | sed 's/|$/\n/'

This text is to show an exampl|e of what im looking for, to b|reak the text into parts conta|ining maximum 30 characters.

sed is to delete the last "|", note that '\n' substitution is not supported in all seds. If you have fever than 20 segments you can do the same with

$ fold -w30 longline | pr -20ts'|'

Also with the -s option you can set the breakpoints at spaces, might be better for human consumption

$ fold -w30 -s longline

This text is to show an
example of what im looking
for, to break the text into
parts containing maximum 30
characters.

vs

$ fold -w30 longline

This text is to show an exampl
e of what im looking for, to b
reak the text into parts conta
ining maximum 30 characters.

Upvotes: 2

Related Questions