tags:

views:

59

answers:

2

This IS kind of linked to another question - http://stackoverflow.com/questions/3171552/code-golf-color-highlighting-of-repeated-text

I'm tying to figure out a way of breaking a file into all 'n' characters long groups.

Eg: If a file comprises of ONLY the following text:

ABCDEFGHIJ

And we want it broken into sets of 3, the output should be:

ABC
BCD
CDE
DEF
EFG
FGH
GHI
HIJ

No characters in the file are to be treated any differently from another. ie, a "space" is just another character which should follow the rule above...

+2  A: 

sed:

echo "ABCDEFGHIJ" | sed -n ':a;/^...$/{p;b};s/.../&\n/;P;s/.//;s/\n//;ba'

A more generalized sed version:

num=5; echo "ABCDEFGHIJ" | sed -n ":a;/^.\{$num\}\$/{p;b};s/.\{$num\}/&\n/;P;s/.//;s/\n//;ba"

Bash and ksh:

string="ABCDEFGHIJ"
for ((i=0;i<=${#string}-3;i++)); do echo ${string:i:3}; done

zsh:

string="ABCDEFGHIJ"
for ((i=1;i<=${#string}-2;i++)); do echo $string[i,i+2]; done

sh (specifically Dash):

string='ABCDEFGHIJ'
count=$(seq $((${#string}-2)))
for i in $count; do b="$b?"; done
for i in $count; do b="${b%?}"; echo "${string%$b}"; string="${string#?}"; done

AWK:

echo "ABCDEFGHIJ" | awk -v num=4 '{for (i=1; i<length($0)-num;i++) print substr($0,i,num)}'

Edit: Added a more generalized sed version and an AWK version.

Dennis Williamson
AWESOME! The sed solution works perfectly and with spaces in there too! I figured the 3 dots are for the "number of characters" and tested it out for other "lengths" as well!! Excellent!!
RubiCon10
Just wondering - isnt there a more straightforward solution with 'dd' and a loop?
RubiCon10
@RubiCon10: `dd`? Huh? I suppose you could duct-tape something together, but I'd use one of the shell loops in my answer before I'd try to do that. I wouldn't use a road grader to till my flower beds, either. See my edit for a more generalized `sed` version and an AWK version.
Dennis Williamson
A: 

Does it have to be shell based or are you open to other scripting languages? Here's a version in Python:

width = 3
data = open("file").read()
for x in xrange(len(data) - width + 1):
    print data[x : x+width]
R Samuel Klatchko