Results 1 to 10 of 10
Is it possible to split a word in to letters?
Eg:
echo "word"
<any command>
[output]
word
w
o
r
d...
- 12-09-2008 #1Just Joined!
- Join Date
- Nov 2008
- Posts
- 37
Bash Scripting: Can i split a word in to letter?
Is it possible to split a word in to letters?
Eg:
echo "word"
<any command>
[output]
word
w
o
r
d
- 12-09-2008 #2Linux User
- Join Date
- Jun 2007
- Posts
- 318
Yes you can, look at the Parameter Expansion section in the bash manpage for:
${parameter:offset:length}
You'd have to set up a loop for the length of the word which can be determined with:
${#parameter}
- 12-09-2008 #3Just Joined!
- Join Date
- Nov 2008
- Posts
- 37
Could you please give me an example for the word "word"
- 12-09-2008 #4
sky_knight02, why don't you give it a shot? If it doesn't work and you have specific questions about your script, post the script here and ask your question.
We're not here to do the work for you, but we are here to help.--
Bill
Old age and treachery will overcome youth and skill.
- 12-09-2008 #5Just Joined!
- Join Date
- Nov 2008
- Posts
- 37
Bill thanks for your reply,
Actually i didnt understand what is meant by offset and what is the syntax, for that only i asked a simple example.
I read bash man but i didnt understand how to call letter by letter from a word.
- 12-09-2008 #6Just Joined!
- Join Date
- Nov 2008
- Posts
- 37
- 12-09-2008 #7Linux User
- Join Date
- Jun 2007
- Posts
- 318
Offset means the number of letters to skip over. In your example you specified to skip over 2 letters 'he'. Since you didn't specify the length to return you got the rest of the word 'llo'.
Try this example. It'll print the length of 'hello' and extract the 1st 2 letters by using offest 0 and then offset 1. The 1 at the end of each means to return one letter.
Code:a=hello echo ${#a} echo ${a:0:1} echo ${a:1:1}
- 12-09-2008 #8Just Joined!
- Join Date
- Nov 2008
- Posts
- 37
Thanks vsemaska now i understood... this helped me a lot....
- 12-10-2008 #9Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Also methods like:
cheers, drlCode:echo word | sed 's/\(.\)/\1\n/g'
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 12-10-2008 #10Just Joined!
- Join Date
- Nov 2008
- Posts
- 37
Thanks drl... that was great.... i think i need to spend some time on sed...
Ill soon post the result of this help in here....


Reply With Quote
