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

2 Answers 2

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

Another shot:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .