Browse > Home / Bash / BASH: Split a string without 'cut' or 'awk'

| Subcribe via RSS

BASH: Split a string without 'cut' or 'awk'

April 10th, 2006 Posted in Bash

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

27 Responses to “BASH: Split a string without 'cut' or 'awk'”

  1. mads Says:

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


  2. kerpz Says:

    very nice.
    Tnx for sharing …


  3. Tako Says:

    Brilliant. Solved a big one for me


  4. Anonymous Says:

    you rock.


  5. RAGHU Says:

    EXCELENT, FANTASTIC, DONE


  6. Hameed Says:

    Just what I needed!


  7. bart Says:

    very nice, used it for a simply archive handling bash script i wrote and put on my site :)


  8. nihilist Says:

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


  9. Anton Says:

    nihilist,

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


  10. Acácio Says:

    Outstanding!

    Very usefull when splitting configuration files like:

    PARAM=VALUE
    PARAM=VALUE
    .
    .
    .

    Congratulations!


  11. Jake Says:

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


  12. MMx Says:

    @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.


  13. oli Says:

    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


  14. v01d Says:

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


  15. Anton Says:

    @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.


  16. Jesper Says:

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


  17. Mark Says:

    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#*;*;}


  18. et lux in tenebris lucet Says:

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


  19. Federico Says:

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


  20. jerik Says:

    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


  21. steadyonabix Says:

    Very elegant, you are no bookmarked!

    Many thanks


  22. Tim K Says:

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


  23. red_team316 Says:

    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.


  24. eureka tips Says:

    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


  25. Jim Hertzler Says:

    #!/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]}"


  26. Anton Says:

    Jim,

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


  27. Jim Hertzler Says:

    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]}


Leave a Reply