How do I split a string on a delimiter in Bash?

后端 未结 30 1913
萌比男神i
萌比男神i 2020-11-21 04:58

I have this string stored in a variable:

IN=\"bla@some.com;john@home.com\"

Now I would like to split the strings by ; delimite

相关标签:
30条回答
  • 2020-11-21 05:21

    This worked for me:

    string="1;2"
    echo $string | cut -d';' -f1 # output is 1
    echo $string | cut -d';' -f2 # output is 2
    
    0 讨论(0)
  • 2020-11-21 05:22

    Use the set built-in to load up the $@ array:

    IN="bla@some.com;john@home.com"
    IFS=';'; set $IN; IFS=$' \t\n'
    

    Then, let the party begin:

    echo $#
    for a; do echo $a; done
    ADDR1=$1 ADDR2=$2
    
    0 讨论(0)
  • 2020-11-21 05:23

    I've seen a couple of answers referencing the cut command, but they've all been deleted. It's a little odd that nobody has elaborated on that, because I think it's one of the more useful commands for doing this type of thing, especially for parsing delimited log files.

    In the case of splitting this specific example into a bash script array, tr is probably more efficient, but cut can be used, and is more effective if you want to pull specific fields from the middle.

    Example:

    $ echo "bla@some.com;john@home.com" | cut -d ";" -f 1
    bla@some.com
    $ echo "bla@some.com;john@home.com" | cut -d ";" -f 2
    john@home.com
    

    You can obviously put that into a loop, and iterate the -f parameter to pull each field independently.

    This gets more useful when you have a delimited log file with rows like this:

    2015-04-27|12345|some action|an attribute|meta data
    

    cut is very handy to be able to cat this file and select a particular field for further processing.

    0 讨论(0)
  • Okay guys!

    Here's my answer!

    DELIMITER_VAL='='
    
    read -d '' F_ABOUT_DISTRO_R <<"EOF"
    DISTRIB_ID=Ubuntu
    DISTRIB_RELEASE=14.04
    DISTRIB_CODENAME=trusty
    DISTRIB_DESCRIPTION="Ubuntu 14.04.4 LTS"
    NAME="Ubuntu"
    VERSION="14.04.4 LTS, Trusty Tahr"
    ID=ubuntu
    ID_LIKE=debian
    PRETTY_NAME="Ubuntu 14.04.4 LTS"
    VERSION_ID="14.04"
    HOME_URL="http://www.ubuntu.com/"
    SUPPORT_URL="http://help.ubuntu.com/"
    BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
    EOF
    
    SPLIT_NOW=$(awk -F$DELIMITER_VAL '{for(i=1;i<=NF;i++){printf "%s\n", $i}}' <<<"${F_ABOUT_DISTRO_R}")
    while read -r line; do
       SPLIT+=("$line")
    done <<< "$SPLIT_NOW"
    for i in "${SPLIT[@]}"; do
        echo "$i"
    done
    

    Why this approach is "the best" for me?

    Because of two reasons:

    1. You do not need to escape the delimiter;
    2. You will not have problem with blank spaces. The value will be properly separated in the array!

    []'s

    0 讨论(0)
  • 2020-11-21 05:24
    echo "bla@some.com;john@home.com" | sed -e 's/;/\n/g'
    bla@some.com
    john@home.com
    
    0 讨论(0)
  • You can set the internal field separator (IFS) variable, and then let it parse into an array. When this happens in a command, then the assignment to IFS only takes place to that single command's environment (to read ). It then parses the input according to the IFS variable value into an array, which we can then iterate over.

    IFS=';' read -ra ADDR <<< "$IN"
    for i in "${ADDR[@]}"; do
        # process "$i"
    done
    

    It will parse one line of items separated by ;, pushing it into an array. Stuff for processing whole of $IN, each time one line of input separated by ;:

     while IFS=';' read -ra ADDR; do
          for i in "${ADDR[@]}"; do
              # process "$i"
          done
     done <<< "$IN"
    
    0 讨论(0)
提交回复
热议问题