I would like to split a string into substrings, separated by some separator (which is also a string itself).
How can I do that
-
using bash only? (for minimalism, and my main interest)
-
or If allowing some text processing program? (for convenience when the program is available)
Thanks.
Simple example,
- split
1--123--23
by--
into1
,123
and23
. - split
1?*123
by?*
into1
and123
Best Answer
Pure bash solution, using
IFS
andread
. Note that the strings shouldn't contain$'\2'
(or whatever else you use for IFS, unfortunately$'\0'
doesn't work, but e.g.$'\666'
does):Or use Perl: