How to split a string with no delimiter

Hi;

I want to write a shell script that will split a string with no delimiter.
Basically the script will read a line from a file.

For example the line it read from the file contains:
99234523

These values are never the same but the length will always be 8.

How do i split this string into 4 sections of 2?

Thanks

Try:

perl -pe 's/(..)/\1 /g' file
% fold -2 infile
99
23
45
23

Bash, ksh93, ...

while read line
do
      a=${line:0:2}
      b=${line:2:2}
      c=${line:4:2}
      d=${line:6:2}
done

Sorry forgot to say this will be a shell script

Thanks

But what shell?

echo "99234523" |awk 'BEGIN{FIELDWIDTHS="2 2 2 2"}{print $1 RS $2 RS $3 RS $4}'
99
23
45
23

+1 for Radulov :smiley:

another way ...heavy but fun :

echo "99234523" | sed 's/../& /g' | xargs -n1

A portable, sh-only approach, in case that is desired (it's not pretty but it should be efficient):

#!/bin/sh

while IFS= read -r line; do
    word1=${line%??????}
    temp=${line%????}
    word2=${temp#??}
    temp=${line%??}
    word3=${temp#????}
    word4=${line#??????}
    printf '%s %s %s %s\n' "$word1" "$word2" "$word3" "$word4"
done

If, instead, each word should be on its own line, change the printf statement to:

printf '%s\n' "$word1" "$word2" "$word3" "$word4"

Regards,
Alister