I have a huge string like
ABCDEFGHIJKLM...
and I would like to split it into substrings of length 5 in this way:
>1
ABCDE
>2
BCDEF
>3
CDEFG
[...]
${string:position:length}
Extracts
$length
characters of substring from$string
at$position
.stringZ=abcABC123ABCabc # 0123456789..... # 0-based indexing. echo ${stringZ:0} # abcABC123ABCabc echo ${stringZ:1} # bcABC123ABCabc echo ${stringZ:7} # 23ABCabc echo ${stringZ:7:3} # 23A # Three characters of substring.
-- from Manipulating Strings in the Advanced Bash-Scripting Guide by Mendel Cooper
Then use a loop to go through and add 1 to the position to extract each substring of length 5.
end=$(( ${#stringZ} - 5 ))
for i in $(seq 0 $end); do
echo ${stringZ:$i:5}
done
fold -w5
should do the trick.
$ echo "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | fold -w5
ABCDE
FGHIJ
KLMNO
PQRST
UVWXY
Z
Cheers!
sed can do it in one shot:
$ echo "abcdefghijklmnopqr"|sed -r 's/(.{5})/\1 /g'
abcde fghij klmno pqr
or
depends on your needs:
$ echo "abcdefghijklmnopqr"|sed -r 's/(.{5})/\1\n/g'
abcde
fghij
klmno
pqr
update
i thought it was just simply split string problem, didn't read the question very carefully. Now it should give what you need:
still one shot, but with awk this time:
$ echo "abcdefghijklmnopqr"|awk '{while(length($0)>=5){print substr($0,1,5);gsub(/^./,"")}}'
abcde
bcdef
cdefg
defgh
efghi
fghij
ghijk
hijkl
ijklm
jklmn
klmno
lmnop
mnopq
nopqr
...or use the split
command:
$ ls
$ echo "abcdefghijklmnopqr" | split -b5
$ ls
xaa xab xac xad
$ cat xaa
abcde
split
also operates on files...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With