Skip to main content

Split string based on delimiter in Bash?


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



I have this string stored in a variable:




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



Now I would like to split the strings by ';' delimiter so that I have




ADDR1="bla@some.com"
ADDR2="john@home.com"



I don't necessarily need the ADDR1 and ADDR2 variables. If they are elements of an array that's even better.



Edit : After suggestions from the answers below, I ended up with the following which is what I was after:




#!/usr/bin/env bash

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

arr=$(echo $IN | tr ";" "\n")

for x in $arr
do
echo "> [$x]"
done



output:




> [bla@some.com]
> [john@home.com]



Edit2 : There was a solution involving setting Internal_field_separator (IFS) to ';'. I am not sure what happened with that answer, how do you reset IFS back to default?



Edit3 : RE: IFS solution, I tried this and it works, I keep the old IFS and then restore it:




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

OIFS=$IFS
IFS=';'
arr2=$IN
for x in $arr2
do
echo "> [$x]"
done

IFS=$OIFS



BTW, when I tried




arr2=($IN)



I only got the first string when printing it in loop, without brackets around $IN it works.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. 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"

    ReplyDelete
  2. If you don't mind processing them immediately, I like to do this:

    for i in $(echo $IN | tr ";" "\n")
    do
    # process
    done


    You could use this kind of loop to initialize an array, but there's probably an easier way to do it. Hope this helps, though.

    ReplyDelete
  3. Taken from Bash shell script split array:

    IN="bla@some.com;john@home.com"
    arrIN=(${IN//;/ })

    ReplyDelete
  4. How about this approach:

    IN="bla@some.com;john@home.com"
    set -- "$IN"
    IFS=";"; declare -a Array=($*)
    echo "${Array[@]}"
    echo "${Array[0]}"
    echo "${Array[1]}"


    Source

    ReplyDelete
  5. echo "bla@some.com;john@home.com" | sed -e 's/;/\n/g'
    bla@some.com
    john@home.com

    ReplyDelete
  6. A different take on Darron's answer, this is how I do it:

    IN="bla@some.com;john@home.com"
    read ADDR1 ADDR2 <<<$(IFS=";"; echo $IN)

    ReplyDelete
  7. You may also:

    dirList=(
    some
    list
    of
    elements
    )

    for i in ${dirList[@]}; do
    ...
    done

    ReplyDelete
  8. How about this one liner, if you're not using arrays:

    IFS=';' read ADDR1 ADDR2 <<<$IN

    ReplyDelete
  9. There are two simple methods:

    cat "text1;text2;text3" | tr " " "\n"


    and

    cat "text1;text2;text3" | sed -e 's/ /\n/g'

    ReplyDelete
  10. This is the simplest way to do it.

    spo='one;two;three'
    OIFS=$IFS
    IFS=';'
    spo_array=($spo)
    IFS=$OIFS
    echo ${spo_array[*]}

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...