Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Converting string to array using BASH?

  1. #1
    Join Date
    Aug 2007
    Beans
    2

    Converting string to array using BASH?

    I was wondering is there was any way to turn a string into an array such that the first letter would be array[0] etc using BASH?

  2. #2
    Join Date
    Jul 2006
    Beans
    Hidden!

    Re: Converting string to array using BASH?

    You can convert to an array:
    Code:
    s="foobar"
    e=$(echo $s | sed 's/\(.\)/\1 /g')
    a=($e)
    
    echo "e = \"$e\""
    echo "a[*] = \"${a[*]}\""
    for i in $(seq 0 $((${#a[*]} - 1))); do
    	echo "a[$i] = \"${a[$i]}\""
    done
    prints:
    Code:
    e = "f o o b a r "
    a[*] = "f o o b a r"
    a[0] = "f"
    a[1] = "o"
    a[2] = "o"
    a[3] = "b"
    a[4] = "a"
    a[5] = "r"
    or simply use single characters from the string:
    Code:
    s="foobar"
    
    for i in $(seq 0 $((${#s} - 1))); do
    	echo "s[$i] = \"${s:$i:1}\""
    done
    prints:
    Code:
    s[0] = "f"
    s[1] = "o"
    s[2] = "o"
    s[3] = "b"
    s[4] = "a"
    s[5] = "r"

  3. #3
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Converting string to array using BASH?

    Quote Originally Posted by heavensCatacomb View Post
    I was wondering is there was any way to turn a string into an array such that the first letter would be array[0] etc using BASH?
    if you just want to use the first letter, you can do substrings
    Code:
    # s="astring"
    # echo ${s:0:1}
    a

  4. #4
    Join Date
    Aug 2007
    Beans
    2

    Re: Converting string to array using BASH?

    Quote Originally Posted by maleficent View Post
    You can convert to an array:
    Code:
    s="foobar"
    e=$(echo $s | sed 's/\(.\)/\1 /g')
    a=($e)
    
    echo "e = \"$e\""
    echo "a[*] = \"${a[*]}\""
    for i in $(seq 0 $((${#a[*]} - 1))); do
    	echo "a[$i] = \"${a[$i]}\""
    done
    This should do nicely thank you.

    As I am fairly new to shell scripting, could you possibly give me a quick rundown on what each part of the script does? Most specifically this part:

    Code:
    for i in $(seq 0 $((${#a[*]} - 1))); do
    	echo "a[$i] = \"${a[$i]}\""
    done

  5. #5
    Join Date
    Jul 2006
    Beans
    Hidden!

    Re: Converting string to array using BASH?

    ${a[*]} refers to all elements in the array, ${#a[*]} is the count of all elements in the array, which, here, is 6, $((6 - 1)) is of course 5, and $(seq 0 5) outputs "0 1 2 3 4 5", then for each of these numbers as $i, the array element ${a[$i]} is echoed.

    I think it's easier to use substrings, as both ghostdog and I mentioned though, ${variable:start:count} syntax.

    edit: it's the 'a=($e)' line that actually creates the array. A static example without the string expansion would be:
    Code:
    a=(f o o b a r)
    Last edited by maleficent; December 20th, 2007 at 03:45 PM.

  6. #6
    Join Date
    Oct 2006
    Location
    Baltimore, MD
    Beans
    667

    Re: Converting string to array using BASH?

    is there a substring like command for words (aka broken by spaces...though that could lead to problems I'll have to deal with...) rather than characters?

    THANKS!

    ----------------------------------------------------------
    EDIT:
    nevermind, I took another look at the static example and realized that was exactly what I was looking for....now, I just have to find a way to present that data in a webpage...

    and before anybody says it, yes that is easier to do in PHP and I've already done it in PHP, I just want to learn bash by repeating it in bash. My string is coming from a MySQL query, for anybody curious.
    Last edited by DouglasAWh; January 4th, 2008 at 05:14 AM. Reason: another look
    I am not a lawyer...yet.
    Music Manumit Podcast - remixable Creative Commons music!
    My neglected blog: http://douglasawh.wordpress.com
    My website: http://opensourceplayground.org

  7. #7
    Join Date
    Oct 2006
    Location
    Baltimore, MD
    Beans
    667

    Re: Converting string to array using BASH?

    Code:
    for i in $(seq 0 $((${#a[*]} - 1))); do
    	echo "a[$i] = \"${a[$i]}\""
    done
    I can't seem to get the syntax right for an if statement after a for. I do have a semicolon after the for statement.
    I am not a lawyer...yet.
    Music Manumit Podcast - remixable Creative Commons music!
    My neglected blog: http://douglasawh.wordpress.com
    My website: http://opensourceplayground.org

  8. #8
    Join Date
    Jun 2009
    Beans
    1

    Re: Converting string to array using BASH?

    Quote Originally Posted by maleficent View Post
    You can convert to an array:
    Code:
    s="foobar"
    e=$(echo $s | sed 's/\(.\)/\1 /g')
    a=($e)
     
    echo "e = \"$e\""
    echo "a[*] = \"${a[*]}\""
    for i in $(seq 0 $((${#a[*]} - 1))); do
        echo "a[$i] = \"${a[$i]}\""
    done
    prints:
    Code:
    e = "f o o b a r "
    a[*] = "f o o b a r"
    a[0] = "f"
    a[1] = "o"
    a[2] = "o"
    a[3] = "b"
    a[4] = "a"
    a[5] = "r"
    Hi, I am new to linux and ubuntu. And I need some help.
    Searching the web for an answer I found this forum. It really is helpful. But I have a problem.
    I need to put a string to an array for a homework that I have, but I can not.
    I tried this exactly as given, but it does not give me the same result.
    It prints:
    Code:
    e = "f o o b a r "
    a[*] = "f o o b a r"
    a[0] = "f o o b a r"
    Please I really need help.. I don't understant what I do wrong.

  9. #9
    Join Date
    Mar 2008
    Beans
    2

    Re: Converting string to array using BASH?

    This is a solution without using any external programs:

    You get the length from ${#STRING} and access the characters using ${STRING:$COUNT:1} where $COUNT is your loop counter

    for ((COUNT=0; COUNT<${#STRING}; COUNT++))
    do
    RECORD[$COUNT]=${STRING:$COUNT:1}
    done

    echo $STRING
    one two three

    echo ${RECORD[0]}
    o
    echo ${RECORD[1]}
    n
    . . .

  10. #10
    Join Date
    Nov 2009
    Beans
    1,081

    Re: Converting string to array using BASH?

    Why are you digging up a ~1.5-year-old thread?

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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