bash
Splitting strings
Taken from http://antonolsen.com/2006/04/10/bash-split-a-string-without-cut-or-awk/
BASH: Split a string without ‘cut’ or ‘awk’, that is without the use of non shell commands. This is useful for portability.
#!/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
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.
Last modified 14 years ago
Last modified on May 13, 2007, 11:20:32 PM