unix - shell - Insert a character at different indexes in a string -
it part of larger script needs shell scripted. simple task in other languages, i'm having trouble accomplishing in shell. have string , want insert "." @ possible indices within string. output can on newlined or separated spaces. can help?
example:
input: "abcd"
output: ".abcd
a.bcd
ab.cd
abc.d
abcd."
or
output: ".abcd a.bcd ab.cd abc.d abcd."
a simple loop do:
input=abcd ((i=0; i<${#input}+1; i++)) echo ${input::$i}.${input:$i} done
this slices string @ each index , inserts .
. can change echo
else appending array if want store them instead ouf output them, of course.
Comments
Post a Comment