str=ABCDEFGHIJKLM splitfive(){ echo "${1:$2:5}" ; } for (( i=0 ; i < ${#str} ; i++ )) ; do splitfive "$str" $i ; done
Or maybe you want to do something more intelligent with the results
#!/usr/bin/env bash splitstr(){ printf '%s\n' "${1:$2:$3}" } n=$1 offset=$2 declare -a by_fives while IFS= read -r str ; do for (( i=0 ; i < ${#str} ; i++ )) ; do by_fives=("${by_fives[@]}" "$(splitstr "$str" $i $n)") done done echo ${by_fives[$offset]}
And then call him
$ split-by 5 2 <<<"ABCDEFGHIJKLM" CDEFG
You can adapt it from there.
EDIT: trivial version in C, for performance comparison:
#include <stdio.h> int main(void){ FILE* f; int n=0; char five[6]; five[5] = '\0'; f = fopen("inputfile", "r"); if(f!=0){ fread(&five, sizeof(char), 5, f); while(!feof(f)){ printf("%s\n", five); fseek(f, ++n, SEEK_SET); fread(&five, sizeof(char), 5, f); } } return 0; }
Forgive my bad C, I really do not speak the language.
source share