Asked  10 Months ago    Answers:  5   Viewed   5 times

I need to split a string into an array. My problem is that the delimiter is a 3 character one: _-_
For example:

db2-111_-_oracle12cR1RAC_-_mariadb101

I'd need to create the following array:

db2-111
oracle12cR1RAC
mariadb101

Similar questions followed this approach:

str="db2-111_-_oracle12cR1RAC_-_mariadb101"
arr=(${str//_-_/ })
echo ${arr[@]}

Even if the array is created, it has been split uncorrectly:

db2 
111 
oracle12cR1RAC 
mariadb101

It seems that the "-" character in the first item causes the array's split function to fail. Can you suggest a fix for it? Thanks

 Answers

1

If you can, replace the _-_ sequences with another single character that you can use for field splitting. For example,

$ str="db2-111_-_oracle12cR1RAC_-_mariadb101"
$ str2=${str//_-_/#}
$ IFS="#" read -ra arr <<< "$str2"
$ printf '%sn' "${arr[@]}"
db2-111
oracle12cR1RAC
mariadb101
Friday, August 27, 2021
 
Lime
 
1
"cat".split("(?!^)")

This will produce

array ["c", "a", "t"]

Tuesday, June 1, 2021
2

In order to convert a string into an array, please use

arr=($line)

or

read -a arr <<< $line

It is crucial not to use quotes since this does the trick.

Friday, June 11, 2021
 
3
$array = str_split("0123456789bcdfghjkmnpqrstvwxyz");

str_split takes an optional 2nd param, the chunk length (default 1), so you can do things like:

$array = str_split("aabbccdd", 2);

// $array[0] = aa
// $array[1] = bb
// $array[2] = cc  etc ...

You can also get at parts of your string by treating it as an array:

$string = "hello";
echo $string[1];

// outputs "e"
Tuesday, June 22, 2021
 
cbcp
 
1
# REVISION: 2017-03-14
# Use of read and other bash specific features (bashisms)

Because parentheses are used to delimit an array, not a string:

ids="1 2 3 4";echo ${ids// /|}
1|2|3|4

Some samples: Populating $ids with two strings: a b and c d

ids=("a b" "c d")

echo ${ids
  • // /|} a|b c|d IFS='|';echo "${ids
  • }";IFS=$' tn' a b|c d
  • ... and finally:

    IFS='|';echo "${ids
    
  • // /|}";IFS=$' tn' a|b|c|d
  • Where array is assembled, separated by 1st char of $IFS, but with space replaced by | in each element of array.

    When you do:

    id="${ids[@]}"
    

    you transfer the string build from the merging of the array ids by a space to a new variable of type string.

    Note: when "${ids[@]}" give a space-separated string, "${ids

  • }" (with a star * instead of the at sign @) will render a string separated by the first character of $IFS.

    what man bash says:

    man -Len -Pcol -b bash | sed -ne '/^ *IFS /{N;N;p;q}'
       IFS    The  Internal  Field  Separator  that  is used for word splitting
              after expansion and to split  lines  into  words  with  the  read
              builtin command.  The default value is ``<space><tab><newline>''.
    

    Playing with $IFS:

    set | grep ^IFS=
    IFS=$' tn'
    

    declare -p IFS
    declare -- IFS=" 
    "
    printf "%qn" "$IFS"
    $' tn'
    

    Literally a space, a tabulation and (meaning or) a line-feed. So, while the first character is a space. the use of * will do the same as @.

    But:

    {
    

        # OIFS="$IFS"
        # IFS=$': tn'
        # unset array 
        # declare -a array=($(echo root:x:0:0:root:/root:/bin/bash))
    

        IFS=: read -a array < <(echo root:x:0:0:root:/root:/bin/bash)
    
        echo 1 "${array[@]}"
        echo 2 "${array
  • }" OIFS="$IFS" IFS=: echo 3 "${array[@]}" echo 4 "${array
  • [*]}" IFS="$OIFS" } 1 root x 0 0 root /root /bin/bash 2 root x 0 0 root /root /bin/bash 3 root x 0 0 root /root /bin/bash 4 root:x:0:0:root:/root:/bin/bash

    Note: The line IFS=: read -a array < <(...) will use : as separator, without setting $IFS permanently. This is because output line #2 present spaces as separators.

    Monday, June 28, 2021
     
    EzzDev
     
    Only authorized users can answer the question. Please sign in first, or register a free account.
    Not the answer you're looking for? Browse other questions tagged :