Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, I have a string which is an IP Address, I want to spli the octets into an array and retrieve the individual octets. Here is what I have got ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Posts
    33

    Bash split string into array

    Hi,

    I have a string which is an IP Address, I want to spli the octets into an array and retrieve the individual octets. Here is what I have got so far:

    Code:
    # create the string IP address
    IPSTR="192.168.1.4"
    
    # declare the array
    declare -a IPARR
    
    IP_ARR=$(echo $IPSTR | tr "." " ")
    
    # print out the first octet (should be 192). This actually prints out a blank line
    echo ${IPARR[0]}
    
    # also tried with quotes, but same result:
    echo "${IPARR[0]}"
    I can print the entire array out with this line of code:
    Code:
    echo ${IPARR[@]}
    I can also loop through the array and print out the octets one by one. But I want to be able to access them directly without having to loop.

    Anyone know why it prints out a blank line when I try to access an array element?

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    ip="192.168.0.1"
    IFS="."
    set -- $ip
    echo "$1,$2,$3,$4"

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Posts
    33
    Thanks ghostdog, I've seen that method from searching. I was hoping to be able to achieve this by using tr method.

    I think it is an issue with the array keys. After the string being split into the array, it doesn't have keys of 0,1,2,3 as I would expect. It does have keys though but I can't see what they are, I know this because when I do a "for in" loop, the data is retrieved.

    Perhaps it's the array declaration needing some syntax to say it's a numerical key array.

  4. #4
    Linux Newbie
    Join Date
    Sep 2005
    Location
    CZ
    Posts
    164

    Talking

    Code:
    # create the string IP address
    IPSTR="192.168.1.4"
    
    # declare the array
    declare -a IPARR
    
    IPARR=(`echo $IPSTR | tr "." " "`)
    
    # print out the first octet (should be 192). This actually prints out a blank line
    echo ${IPARR[0]}
    
    # also tried with quotes, but same result:
    echo "${IPARR[0]}"
    Works for me...

    You should slap yourself...


    Or:
    1. Read carefully, IP_ARR is not the same as IPARR
    2. Array should be initialized with (content1 content2)
    3. Code:
      #! /bin/bash -x
      is useful when debugging your scripts. After your debug process is done, you can remove the -x parameter


  5. #5
    Just Joined!
    Join Date
    Jun 2007
    Posts
    12
    Don't use a pipe as it causes a new process to be created. Here is your code with a small change. Use "IPARR=(`echo ${IPSTR//./ }`)" instead of "IPARR=(`echo $IPSTR | tr "." " "`)"

    # create the string IP address
    IPSTR="192.168.1.4"

    # declare the array
    declare -a IPARR

    IPARR=(`echo ${IPSTR//./ }`)

    # print out the first octet (should be 192). This actually prints out a blank line
    echo ${IPARR[0]}

    # also tried with quotes, but same result:
    echo "${IPARR[0]}"

Posting Permissions

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