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
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.
[...]
mapfile -td ';' <<< "$foo"
Caveats:
1) You can only have one, single character delimiter.
But you could have more using IFS.
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.
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=)
Kenny McCormack <[email protected]> wrote:
mapfile -td ';' < <(someprocess | awk 1 ORS=)
mapfile -t < <(someprocess | sed 's/;/\n/g')
To add another thought to the original question, and since I know
that the OP already has the relevant experience for that; for such
a basic function writing a shell built-in could be appropriate.
(That's not portable, but I think the OP doesn't care about that.)
[email protected] (Kenny McCormack) writes:
Kenny McCormack <[email protected]> wrote:
mapfile -td ';' < <(someprocess | awk 1 ORS=)
[...]
mapfile -t < <(someprocess | sed 's/;/\n/g')
And in your original post you wrote:
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).
So why bother with a shell solution and why bother with avoiding IFS,
when in the end you need to resort to AWK/sed anyway?
Do not get me wrong, I am learning a lot in this thread here, much of
the stuff is far beyond my level of expertise in shell programming, and
it would be great to have a shell-only solution for your inquiry, even
if only for "academic reasons" because, say, the solution (still to
come) may turn out to be too clumsy for daily use). I will applaud such
a result, but for the time being I would be happy if you could elaborate somewhat more about your motivation for this exercise.
Best regards
Axel
In fact, one of my motivations for posting is the hope that a bash dev
might see this and be motivated to add the functionality to the shell.
Then I wouldn't have to write it myself.
That and/or be motivated to fix the IFS handling in the shell.
IFS is the way to go.
On Sun, 10 Nov 2024 00:51:58 -0000 (UTC), Lem Novantotto wrote:
But you could have more using IFS.
IFS is the way to go.
If you want your change to IFS to be only temporary, you can restrict it
to a subshell by putting the code sequence in ???( ... )???. But then you cannot pass variables back to the parent shell.
Another option is to use a coproc command.
[email protected] (Kenny McCormack) writes:
Kenny McCormack <[email protected]> wrote:
mapfile -td ';' < <(someprocess | awk 1 ORS=)
[...]
mapfile -t < <(someprocess | sed 's/;/\n/g')
And in your original post you wrote:
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).
So why bother with a shell solution and why bother with avoiding IFS,
when in the end you need to resort to AWK/sed anyway?
Do not get me wrong, I am learning a lot in this thread here, much of
the stuff is far beyond my level of expertise in shell programming, and
it would be great to have a shell-only solution for your inquiry, even
if only for "academic reasons" because, say, the solution (still to
come) may turn out to be too clumsy for daily use). I will applaud such
a result, but for the time being I would be happy if you could elaborate >somewhat more about your motivation for this exercise.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 714 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 136:12:06 |
| Calls: | 12,087 |
| Files: | 14,997 |
| Messages: | 6,517,374 |