Find the answer to your Linux question:
Results 1 to 3 of 3
I am using gnu bash 3.2 I need to split the string into array like a=this_is_whole_world.file # split [_.] I need to split this on _ and . some thing ...
  1. #1
    Just Joined!
    Join Date
    Oct 2010
    Posts
    1

    bash string splitting into array

    I am using gnu bash 3.2
    I need to split the string into array like

    a=this_is_whole_world.file # split [_.]

    I need to split this on _ and . some thing like this
    a[0]=this
    a[1]=is
    a[2]=whole
    a[3]=world
    a[4]=file

    preferable using bash regex. if not sed is also ok.

    Thanks
    Pen

  2. #2
    Linux User
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    262
    Quote Originally Posted by praveenhm View Post
    I am using gnu bash 3.2
    I need to split the string into array like

    a=this_is_whole_world.file # split [_.]

    I need to split this on _ and . some thing like this
    a[0]=this
    a[1]=is
    a[2]=whole
    a[3]=world
    a[4]=file

    preferable using bash regex. if not sed is also ok.

    Thanks
    Pen
    Code:
    string_var=this_is_whole_world.file
    a=( $(echo "${string_var//[_\.]/ }") )
    
    for i in "${!a[@]}"; do
        printf "%6d) '%s'\n" "$i" "${a[$i]}"
    done
    Results:
    Code:
         0) 'this'
         1) 'is'
         2) 'whole'
         3) 'world'
         4) 'file'

  3. #3
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Code:
    IFS=_. read -a a <<< "$a"

Posting Permissions

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