Skip to content

BASH: Split a string without ‘cut’ or ‘awk’

For a little test script I’m writing I needed to split a line on a ‘;’ but preservere the “s and ‘s, something that echo doesn’t like to do. Digging deeper into the bash docs I see that there are some handy string handling functions.

#!/bin/bash
line=’this “is” a command;this “is” a pattern’
COMMAND=${line%;*}
PATTERN=${line#*;}
echo $COMMAND
echo $PATTERN

And the output would be:

this “is” a command
this “is” a pattern

30 Comments

  1. mads

    This one should win a price… I works great and is noce and simple…

    Posted on 15-Jun-06 at 9:15 am | Permalink
  2. kerpz

    very nice.
    Tnx for sharing …

    Posted on 23-Jul-06 at 11:55 pm | Permalink
  3. Tako

    Brilliant. Solved a big one for me

    Posted on 04-Aug-06 at 9:28 am | Permalink
  4. Anonymous

    you rock.

    Posted on 23-Aug-06 at 2:05 pm | Permalink
  5. RAGHU

    EXCELENT, FANTASTIC, DONE

    Posted on 24-Aug-06 at 7:34 pm | Permalink
  6. Hameed

    Just what I needed!

    Posted on 25-Oct-06 at 12:06 am | Permalink
  7. very nice, used it for a simply archive handling bash script i wrote and put on my site :)

    Posted on 14-Nov-06 at 6:34 pm | Permalink
  8. nihilist

    The only problem is that it doesn’t deal with multiple instancse of the separator character. What then?

    Posted on 06-Feb-07 at 12:25 pm | Permalink
  9. nihilist,

    Then you break out some perl or sed regex action and break it apart accordingly.

    Posted on 13-Feb-07 at 12:27 am | Permalink
  10. Acácio

    Outstanding!

    Very usefull when splitting configuration files like:

    PARAM=VALUE
    PARAM=VALUE
    .
    .
    .

    Congratulations!

    Posted on 22-Feb-07 at 6:04 pm | Permalink
  11. Jake

    This is so much more elegant than a nasty sed/awk expression. Thanks for improving my code!

    Posted on 26-Feb-07 at 6:09 pm | Permalink
  12. MMx

    @nihilist:
    If you have multiple separator characters, like this:

    string1;string2;string3

    then you can split it this way:

    original=’string1;string2;string3′
    part1=${original%%;*}; rest=${original#*;}
    part2=${rest%%;*}; rest=${rest#*;}
    part3=${rest%%;*};

    This should work for arbitrary number of parts.

    Posted on 17-Apr-07 at 8:28 am | Permalink
  13. oli

    very nice … thanks

    How to do if I have 3 tags (or more) like

    line=’this “is” a command;this “is” a pattern;this “is” a bla’
    COMMAND=${line%;*}
    PATTERN=${line#*;}
    echo $COMMAND
    echo $PATTERN

    Posted on 07-Jun-07 at 1:25 am | Permalink
  14. v01d

    nice job! i’ll use it for my scripting. thanks!

    Posted on 07-Jun-07 at 1:39 am | Permalink
  15. @oli:

    I’d just use ‘cut -d”;” -f1′ (and 2 and 3) to get each element then.
    COMMAND=$(echo $line | cut -d”;” -f1)
    PATTERN=$(echo $line | cut -d”;” -f2)
    BLAH=$(echo $line | cut -d”;” -f3)

    I am sure there is a way to split it properly, but I don’t recall how.

    Posted on 11-Jun-07 at 1:04 pm | Permalink
  16. Jesper

    Great! I use it to test if a site is changed (blackboard is down so when it changes I want to know)

    Posted on 20-Jun-07 at 4:35 am | Permalink
  17. Thanks for the way here.
    And I would like to share the way for 3 tag line, I tested this and used it.

    COMMAND=${line%;*;*}
    PATTERN=${line#*;}
    PATTERN=${PATTERN%;*}
    BLAH=${line#*;*;}

    Posted on 19-Jul-07 at 10:10 pm | Permalink
  18. Federico

    Than you very much for this hint! It saved me from some tricky shell scripting :-)

    Posted on 30-Dec-07 at 4:09 am | Permalink
  19. jerik

    If I am right, this just works if you know how much delimiter are in the string you wish to split. Nice would be if this should work without knowing this…

    cheers — jerik

    Posted on 10-Feb-08 at 8:20 am | Permalink
  20. Very elegant, you are no bookmarked!

    Many thanks

    Posted on 17-Mar-08 at 7:43 am | Permalink
  21. Tim K

    Thanks, you saved me lot of time and valuable bytes!

    Posted on 06-Jun-08 at 10:00 am | Permalink
  22. red_team316

    You sir are Awesome! Not that this makes me enjoy bash any more(I prefer real programming languages), but this is much more understandable than that sed/awk stuff.

    Bookmarked, Stumbled, Saved.

    Posted on 24-Aug-08 at 1:57 am | Permalink
  23. This cool tip was a lot helpful one…..I did a srch for my problem of interpreting a .CSV and this works like a charm

    Posted on 01-Sep-08 at 2:53 am | Permalink
  24. Jim Hertzler

    #!/bin/bash
    # Split the command line argument on the colon character.

    IFS=”:”; declare -a Array=($*)

    echo “Array[0]=${Array[0]}”
    echo “Array[1]=${Array[1]}”
    echo “Array[2]=${Array[2]}”
    echo “Array[3]=${Array[3]}”

    Posted on 05-Sep-08 at 9:39 am | Permalink
  25. Jim,

    Thanks! I hadn’t known about the IFS internal variable.

    Posted on 05-Sep-08 at 9:46 am | Permalink
  26. Jim Hertzler

    Go to:

    http://www.linuxquestions.org/questions/programming-9/bash-shell-script-split-array-383848/?posted=1#post3270996

    And see:

    IP=1.2.3.4; IP=(${IP//./ }); Rev=${IP[3]}.${IP[2]}.${IP[1]}.${IP[0]}

    Posted on 05-Sep-08 at 4:25 pm | Permalink
  27. alt

    Here’s yet another take on it:

    Splitting strings in Bash

    http://codesnippets.joyent.com/posts/show/2016

    Posted on 28-May-09 at 6:01 am | Permalink
  28. Hi,

    redefinig IFS did work only on Linux, not on Solaris. So I wrote
    a loop to split the input data into an array.

    #!/bin/bash
    declare -a Array

    original=’string1 with spaces ~string2~string3~;; special chars \\;~last thing’
    rest=$original
    i=0

    #create Array from line separated by ~
    while true; do
    restTmp=$rest
    part1=${rest%%~*}; rest=${rest#*~}
    Array[$i]=”$part1″
    #echo $rest
    i=`expr $i + 1`
    if [ "$rest" == "$restTmp" ]; then
    break
    fi
    done

    #access Array
    echo “Array[0]=${Array[0]}”
    echo “Array[1]=${Array[1]}”
    echo “Array[2]=${Array[2]}”
    echo “Array[3]=${Array[3]}”
    echo “Array[4]=${Array[4]}”

    br

    Harald Strack

    Posted on 29-May-09 at 4:00 am | Permalink
  29. andu

    somethings big things really come in small packages
    great thing :)

    Posted on 20-Dec-09 at 8:37 pm | Permalink
  30. knightEknight

    # Using your original example I derived a way to loop thru a tokenized string:

    #!/bin/bash
    STR=”a,b,cc,ddd,ee,f,gg”
    TOKEN=”"

    echo STR: $STR
    echo “Tokens:”

    until [ "$STR" == "$TOKEN" ]; do
    TOKEN=${STR%%,*}
    STR=${STR#*,}
    echo $TOKEN
    done

    Posted on 30-Jun-10 at 9:55 am | Permalink

One Trackback/Pingback

  1. et lux in tenebris lucet on 05-Nov-07 at 3:03 pm

    Splitting a string in bash…

    I found this little trick to split a string in bash, so I’m sharing. Maybe it’s obvious to some people, but I don’t do a lot of shell scripting, so it was helpful to me.
    http://anton.lr2.com/archives/2006/04/10/bash-split-a-string-wit...

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*