Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split large string into substrings

Tags:

string

bash

shell

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
[...]
like image 482
didymos Avatar asked Sep 27 '11 11:09

didymos


4 Answers

${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
like image 88
chown Avatar answered Nov 17 '22 16:11

chown


fold -w5 should do the trick.

$ echo "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | fold -w5
ABCDE
FGHIJ
KLMNO
PQRST
UVWXY
Z

Cheers!

like image 26
Zack Avatar answered Nov 17 '22 14:11

Zack


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
like image 15
Kent Avatar answered Nov 17 '22 15:11

Kent


...or use the split command:

$ ls

$ echo "abcdefghijklmnopqr" | split -b5

$ ls
xaa  xab  xac  xad

$ cat xaa
abcde

split also operates on files...

like image 2
Fredrik Pihl Avatar answered Nov 17 '22 15:11

Fredrik Pihl