Find the answer to your Linux question:
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...
  1. #1
    Just 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

  2. #2
    Linux 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}

  3. #3
    Just Joined!
    Join Date
    Nov 2008
    Posts
    37
    Could you please give me an example for the word "word"

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.

  5. #5
    Just 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.

  6. #6
    Just 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.
    Quote Originally Posted by sky_knight02 View Post
    a=hello
    echo ${a:2}

    [output]
    llo
    im getting the first two letters delted... but I want to take a particular work from it, how to do it?

  7. #7
    Linux 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}

  8. #8
    Just Joined!
    Join Date
    Nov 2008
    Posts
    37
    Thanks vsemaska now i understood... this helped me a lot....

  9. #9
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Also methods like:
    Code:
    echo word | sed 's/\(.\)/\1\n/g'
    cheers, drl
    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 )

  10. #10
    Just 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....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...