Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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 :)

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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
share|improve this answer
Thanks for the help, worked beautifully :) – Daniel Rodrigues Jul 18 '11 at 22:10

Another shot:

tr -d '\n' <input | sed -r 's/(.{8})/\1\n/g'
share|improve this answer
The sed expression can be simplified to s/.{8}/\0\n/g. – Lekensteyn Jul 18 '11 at 21:53
good, thank you – enzotib Jul 18 '11 at 21:54
Thanks, tried your suggestion too and it did the job :) – Daniel Rodrigues Jul 18 '11 at 22:11
if we're golfing, s/.{8}/&\n/g saves a character – glenn jackman Jul 19 '11 at 22:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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