Kenny McCormack
2024-11-09 16:19:17 UTC
There is a feature that is prominently missing from the shell language (I
am speaking primarily of bash here) - which is the ability to split a
string on a delimiter. This is a common operation in most other
text-processing oriented languages (AWK, Perl, etc).
First note/caveat: I'm not interested in any solution involving IFS, for
two reasons:
1) IFS-based solutions never work for me.
2) Changing IFS is inherently dangerous, because, well, IFS itself in
inherently dangerous. Yes, I know it has been somewhat de-fanged
recently - but it is still dangerous.
Anyway, the point of this thread is that I have recently developed a good
solution for this, using bash's "mapfile" command. Suppose we have a
string in a variable (foo) like: foo;bar;bletch
I.e., with ; as the delimiter.
This works well, with a couple of caveats:
mapfile -td ';' <<< "$foo"
Caveats:
1) You can only have one, single character delimiter. It'd be nice if
you could have a reg-exp, like in GAWK.
2) If the output you're processing comes from a process, as is usually
the case, special care must be taken:
mapfile -td ';' < <(someprocess | awk 1 ORS=)
The point is that since ';' is now the delimiter, the newline at the end of
the line coming from someprocess is not treated as a delimiter, so it must
be disposed of with the AWK script. It'd be nice if you could make both
';' and '\n' be recognized as delimiters.
But other than those two caveats, it works well.
am speaking primarily of bash here) - which is the ability to split a
string on a delimiter. This is a common operation in most other
text-processing oriented languages (AWK, Perl, etc).
First note/caveat: I'm not interested in any solution involving IFS, for
two reasons:
1) IFS-based solutions never work for me.
2) Changing IFS is inherently dangerous, because, well, IFS itself in
inherently dangerous. Yes, I know it has been somewhat de-fanged
recently - but it is still dangerous.
Anyway, the point of this thread is that I have recently developed a good
solution for this, using bash's "mapfile" command. Suppose we have a
string in a variable (foo) like: foo;bar;bletch
I.e., with ; as the delimiter.
This works well, with a couple of caveats:
mapfile -td ';' <<< "$foo"
Caveats:
1) You can only have one, single character delimiter. It'd be nice if
you could have a reg-exp, like in GAWK.
2) If the output you're processing comes from a process, as is usually
the case, special care must be taken:
mapfile -td ';' < <(someprocess | awk 1 ORS=)
The point is that since ';' is now the delimiter, the newline at the end of
the line coming from someprocess is not treated as a delimiter, so it must
be disposed of with the AWK script. It'd be nice if you could make both
';' and '\n' be recognized as delimiters.
But other than those two caveats, it works well.
--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/Cancer
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/Cancer