4

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:

010000100110010100100000011100110111010101110010011001
010010000001110100011011110010000001100100011100100110
100101101110011010110010000001111001011011110111010101
110010001000000100111101110110011000010110110001110100
01101001011011100110010100101110

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

4

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 | |
3

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.