I'm toying around with binary code and ciphers and I've a chunk of text which I would like to reorganise in 8-character lines (those familiar with the topic will understand why). See:


I'm sure I must use cut, split, grep, sort or maybe a combination of these (and/or others), and I've made some tries with all these, but without any success.

I could easily do this manually, but this's what computers were made for, back in the World War II days :)


Assuming you use bash, or some other shell that has a "-n" option for "read"

process to produce text |
tr -d '\n' |
while read -n 8 chars; do echo "$chars"; done
| improve this answer | |

Another shot:

tr -d '\n' <input | sed -r 's/(.{8})/\1\n/g'
| improve this answer | |
  • The sed expression can be simplified to s/.{8}/\0\n/g. – Lekensteyn Jul 18 '11 at 21:53
  • if we're golfing, s/.{8}/&\n/g saves a character – glenn jackman Jul 19 '11 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.