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
This one should win a price… I works great and is noce and simple…
very nice.
Tnx for sharing …
Brilliant. Solved a big one for me
you rock.
EXCELENT, FANTASTIC, DONE
Just what I needed!
very nice, used it for a simply archive handling bash script i wrote and put on my site
The only problem is that it doesn’t deal with multiple instancse of the separator character. What then?
nihilist,
Then you break out some perl or sed regex action and break it apart accordingly.
Outstanding!
Very usefull when splitting configuration files like:
PARAM=VALUE
PARAM=VALUE
.
.
.
Congratulations!
This is so much more elegant than a nasty sed/awk expression. Thanks for improving my code!
Is there a way to get this to work with more than once delimiter present?
Here’s how you could use multiple delimiters.. This is messy and I think there’s a better way but I havent figured it out yet.
#!/bin/bash
line=’content,100,2′
content=${line%%,*}
delay=${line#*,}
delay=${delay%,*}
instance=${line##*,}
echo $content
echo $delay
echo $instance
I would read more here: http://linuxgazette.net/issue18/bash.html
Thuktun: If you double the % or #, then it matches as few as it can instead of as much as it can. Example:
> T=a,b,c,d
> echo ${T%%,*}
a
> echo ${T%,*}
a,b,c