Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"I am, therefore I am." -- Akira


devel / comp.unix.shell / How to do "split" in bash

SubjectAuthor
* How to do "split" in bashKenny McCormack
+* Re: How to do "split" in bashKenny McCormack
|`* Re: How to do "split" in bashLew Pitcher
| `- Re: How to do "split" in bashLew Pitcher
`* Re: How to do "split" in bashKaz Kylheku
 +- Re: How to do "split" in bashKaz Kylheku
 +* Re: How to do "split" in bashKenny McCormack
 |`- Re: How to do "split" in bashJanis Papanagnou
 `- Re: How to do "split" in bashMartijn Dekker

1
How to do "split" in bash

<sis9e1$2konq$1@news.xmission.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4434&group=comp.unix.shell#4434

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: How to do "split" in bash
Date: Mon, 27 Sep 2021 11:20:33 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <sis9e1$2konq$1@news.xmission.com>
Injection-Date: Mon, 27 Sep 2021 11:20:33 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2777850"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Mon, 27 Sep 2021 11:20 UTC

In earlier episodes of our saga, I had complained about the lack of a
"split" function in bash. By "split", I mean taking a string and making an
array out of it, as does the AWK "split" function.

I have now figured out a general solution to this problem. Observe:

$ od -bc <<< "$IFS"
0000000 040 011 012 012
\t \n \n
0000004
$ IFS=',;:' read -a foo <<< 'word 1,that and; ANother string: Foo on you!!!'
$ printf -- "---> %s\n" "${foo[@]}"
---> word 1
---> that and
---> ANother string
---> Foo on you!!!
$ od -bc <<< "$IFS"
0000000 040 011 012 012
\t \n \n
0000004
$

--
The whole aim of practical politics is to keep the populace alarmed (and hence clamorous
to be led to safety) by menacing it with an endless series of hobgoblins, all of them imaginary.

H. L. Mencken

Re: How to do "split" in bash

<sislec$2krlr$1@news.xmission.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4435&group=comp.unix.shell#4435

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: How to do "split" in bash
Date: Mon, 27 Sep 2021 14:45:32 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <sislec$2krlr$1@news.xmission.com>
References: <sis9e1$2konq$1@news.xmission.com>
Injection-Date: Mon, 27 Sep 2021 14:45:32 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2780859"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Mon, 27 Sep 2021 14:45 UTC

In article <sis9e1$2konq$1@news.xmission.com>,
Kenny McCormack <gazelle@shell.xmission.com> wrote:
>In earlier episodes of our saga, I had complained about the lack of a
>"split" function in bash. By "split", I mean taking a string and making an
>array out of it, as does the AWK "split" function.

Note that this same general idea can be used to address another question
that I've wrestled with (and posted about) previously: That is, removing
leading/trailing blanks from a string in bash. Observe:

$ foo=" leading and trailing spaces "
$ read -r bar <<< "$foo"

In fact, you could even do:

$ read -r foo <<< "$foo"

to do it "in place".

--
He must be a Muslim. He's got three wives and he doesn't drink.

Re: How to do "split" in bash

<20210927084957.715@kylheku.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4436&group=comp.unix.shell#4436

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 480-992-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: How to do "split" in bash
Date: Mon, 27 Sep 2021 16:01:03 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <20210927084957.715@kylheku.com>
References: <sis9e1$2konq$1@news.xmission.com>
Injection-Date: Mon, 27 Sep 2021 16:01:03 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="62fc2e1cef0bce9694661d3b2d3b110c";
logging-data="9692"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX198R3YGyDWxlPGq8X5Xuo7vEJAa+/9nW/E="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:68uTO+gDBPWdJ2/dJNM/n++eyJM=
 by: Kaz Kylheku - Mon, 27 Sep 2021 16:01 UTC

On 2021-09-27, Kenny McCormack <gazelle@shell.xmission.com> wrote:
> In earlier episodes of our saga, I had complained about the lack of a
> "split" function in bash. By "split", I mean taking a string and making an
> array out of it, as does the AWK "split" function.
>
> I have now figured out a general solution to this problem. Observe:

You were beaten to it by days by one Abhishek Prakash, who blogged about
it on September 4. It comes up as the first hit in a search for "split
string into array in bash" in ducu

https://linuxhandbook.com/bash-split-string/

Method 1: Split string using read command in Bash

...
my_string="Ubuntu;Linux Mint;Debian;Arch;Fedora"
IFS=';' read -ra my_array <<< "$my_string"

The idea does not originate with Prakash. The following 2019-dated
StackOverflow answer documents the approach also.

https://stackoverflow.com/a/58600824/1250772

Stil blows goats, but its nice not having to save, assign and
restore IFS, at least.

Re: How to do "split" in bash

<20210927090743.59@kylheku.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4437&group=comp.unix.shell#4437

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 480-992-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: How to do "split" in bash
Date: Mon, 27 Sep 2021 16:12:13 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <20210927090743.59@kylheku.com>
References: <sis9e1$2konq$1@news.xmission.com>
<20210927084957.715@kylheku.com>
Injection-Date: Mon, 27 Sep 2021 16:12:13 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="62fc2e1cef0bce9694661d3b2d3b110c";
logging-data="18119"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+5+GRwsVyAmUMLPN/JRSqwtK6yHLyXWwc="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:FV1o88k8CaZ9a8JW9gkkdPUsv/A=
 by: Kaz Kylheku - Mon, 27 Sep 2021 16:12 UTC

On 2021-09-27, Kaz Kylheku <480-992-1380@kylheku.com> wrote:
> The idea does not originate with Prakash. The following 2019-dated
> StackOverflow answer documents the approach also.
>
> https://stackoverflow.com/a/58600824/1250772

Just before closing the page, I noticed that the plagiarist just cribbed
the IFS = ... read -a .. <<< trick from the massively upvoted, accepted
answer to the same question, dated 2012:

https://stackoverflow.com/a/10586169/1250772

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Re: How to do "split" in bash

<sisr3h$2kvhf$1@news.xmission.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4438&group=comp.unix.shell#4438

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: How to do "split" in bash
Date: Mon, 27 Sep 2021 16:22:09 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <sisr3h$2kvhf$1@news.xmission.com>
References: <sis9e1$2konq$1@news.xmission.com> <20210927084957.715@kylheku.com>
Injection-Date: Mon, 27 Sep 2021 16:22:09 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2784815"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Mon, 27 Sep 2021 16:22 UTC

In article <20210927084957.715@kylheku.com>,
Kaz Kylheku <480-992-1380@kylheku.com> wrote:
>On 2021-09-27, Kenny McCormack <gazelle@shell.xmission.com> wrote:
>> In earlier episodes of our saga, I had complained about the lack of a
>> "split" function in bash. By "split", I mean taking a string and making an
>> array out of it, as does the AWK "split" function.
>>
>> I have now figured out a general solution to this problem. Observe:
>
>You were beaten to it by days by one Abhishek Prakash, who blogged about
>it on September 4. It comes up as the first hit in a search for "split
>string into array in bash" in ducu

What makes you think I read any of those websites (Stack*, LinuxWhatever,
....) ?

--
Faith doesn't give you the answers; it just stops you from asking the questions.

Re: How to do "split" in bash

<sisskc$1nur$1@gioia.aioe.org>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4439&group=comp.unix.shell#4439

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!I4G2pI/TjSgw9iO3pv/WTg.user.46.165.242.75.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: How to do "split" in bash
Date: Mon, 27 Sep 2021 18:48:12 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sisskc$1nur$1@gioia.aioe.org>
References: <sis9e1$2konq$1@news.xmission.com>
<20210927084957.715@kylheku.com> <sisr3h$2kvhf$1@news.xmission.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="57307"; posting-host="I4G2pI/TjSgw9iO3pv/WTg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
X-Enigmail-Draft-Status: N1110
X-Notice: Filtered by postfilter v. 0.9.2
 by: Janis Papanagnou - Mon, 27 Sep 2021 16:48 UTC

On 27.09.2021 18:22, Kenny McCormack wrote:
> In article <20210927084957.715@kylheku.com>,
> Kaz Kylheku <480-992-1380@kylheku.com> wrote:
>> On 2021-09-27, Kenny McCormack <gazelle@shell.xmission.com> wrote:
>>> In earlier episodes of our saga, I had complained about the lack of a
>>> "split" function in bash. By "split", I mean taking a string and making an
>>> array out of it, as does the AWK "split" function.
>>>
>>> I have now figured out a general solution to this problem. Observe:
>>
>> You were beaten to it by days by one Abhishek Prakash, who blogged about
>> it on September 4. It comes up as the first hit in a search for "split
>> string into array in bash" in ducu
>
> What makes you think I read any of those websites (Stack*, LinuxWhatever,
> ...) ?

I suppose he supposed that you'd do some searching on "The Internet"
(as often suggested here) before asking.

But would that be necessary at all? Since you seem to have asked that
question also here in September this year. Of course you'll find in a
response of mine also the "IFS=... read..." proposal suggested (and
demonstrated in an example for ksh; the bash manual inspection for
syntax adjustments was suggested for homework).

Janis *shrug*

Re: How to do "split" in bash

<sist5l$3rm$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4440&group=comp.unix.shell#4440

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.shell
Subject: Re: How to do "split" in bash
Date: Mon, 27 Sep 2021 16:57:26 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <sist5l$3rm$1@dont-email.me>
References: <sis9e1$2konq$1@news.xmission.com>
<sislec$2krlr$1@news.xmission.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 27 Sep 2021 16:57:26 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3f08fb3ca904b4189e1ab6c851fb5c83";
logging-data="3958"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18QIdZQ1FJ0LeZDKJKqI9XPl6QHUnibjEQ="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:5miT9NBGhyvyY8ND7OqZE3PiBOE=
 by: Lew Pitcher - Mon, 27 Sep 2021 16:57 UTC

On Mon, 27 Sep 2021 14:45:32 +0000, Kenny McCormack wrote:

> In article <sis9e1$2konq$1@news.xmission.com>,
> Kenny McCormack <gazelle@shell.xmission.com> wrote:
>>In earlier episodes of our saga, I had complained about the lack of a
>>"split" function in bash. By "split", I mean taking a string and making an
>>array out of it, as does the AWK "split" function.
>
> Note that this same general idea can be used to address another question
> that I've wrestled with (and posted about) previously: That is, removing
> leading/trailing blanks from a string in bash. Observe:
>
> $ foo=" leading and trailing spaces "
> $ read -r bar <<< "$foo"
>
> In fact, you could even do:
>
> $ read -r foo <<< "$foo"
>
> to do it "in place".

I suggest that even the "read" is redundant
foo=" leading and trailing spaces "
bar=( $foo }

And, of course, it works with an altered IFS
oldIFS=$IFS
IFS=e bar=$($foo)
IFS=$oldIFS

--
Lew Pitcher
"In Skills, We Trust"

Re: How to do "split" in bash

<sist6n$3rm$2@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4441&group=comp.unix.shell#4441

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.shell
Subject: Re: How to do "split" in bash
Date: Mon, 27 Sep 2021 16:57:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <sist6n$3rm$2@dont-email.me>
References: <sis9e1$2konq$1@news.xmission.com>
<sislec$2krlr$1@news.xmission.com> <sist5l$3rm$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 27 Sep 2021 16:57:59 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3f08fb3ca904b4189e1ab6c851fb5c83";
logging-data="3958"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+WF8XqX5ZAMginl8PJVUsJARJasdKNQTU="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:1qBoO059ASkTRNCQWSwPbSlZU4g=
 by: Lew Pitcher - Mon, 27 Sep 2021 16:57 UTC

On Mon, 27 Sep 2021 16:57:26 +0000, Lew Pitcher wrote:

> On Mon, 27 Sep 2021 14:45:32 +0000, Kenny McCormack wrote:
>
>> In article <sis9e1$2konq$1@news.xmission.com>,
>> Kenny McCormack <gazelle@shell.xmission.com> wrote:
>>>In earlier episodes of our saga, I had complained about the lack of a
>>>"split" function in bash. By "split", I mean taking a string and making an
>>>array out of it, as does the AWK "split" function.
>>
>> Note that this same general idea can be used to address another question
>> that I've wrestled with (and posted about) previously: That is, removing
>> leading/trailing blanks from a string in bash. Observe:
>>
>> $ foo=" leading and trailing spaces "
>> $ read -r bar <<< "$foo"
>>
>> In fact, you could even do:
>>
>> $ read -r foo <<< "$foo"
>>
>> to do it "in place".
>
> I suggest that even the "read" is redundant
> foo=" leading and trailing spaces "
> bar=( $foo }
Typo
bar=( $foo )

>
> And, of course, it works with an altered IFS
> oldIFS=$IFS
> IFS=e bar=$($foo)
> IFS=$oldIFS

--
Lew Pitcher
"In Skills, We Trust"

Re: How to do "split" in bash

<irjm3rFqvi4U1@mid.individual.net>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4442&group=comp.unix.shell#4442

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: mart...@inlv.demon.nl (Martijn Dekker)
Newsgroups: comp.unix.shell
Subject: Re: How to do "split" in bash
Date: Wed, 29 Sep 2021 18:29:30 +0100
Lines: 38
Message-ID: <irjm3rFqvi4U1@mid.individual.net>
References: <sis9e1$2konq$1@news.xmission.com>
<20210927084957.715@kylheku.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net +/I1+JXeafYRJucguGMt3Qziocd/jDgzjSXJ2pCVoX/131GvM=
Cancel-Lock: sha1:Pv53mPqAJ+kkKhHjIC3UN9BexiE=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0)
Gecko/20100101 Thunderbird/78.14.0
In-Reply-To: <20210927084957.715@kylheku.com>
Content-Language: en-GB
 by: Martijn Dekker - Wed, 29 Sep 2021 17:29 UTC

Op 27-09-21 om 18:01 schreef Kaz Kylheku:
> Stil blows goats, but its nice not having to save, assign and
> restore IFS, at least.

To do this safely in shell, we have to not only set IFS to the separator
but also disable pathname expansion (globbing).

On bash 5.0 and higher, you can use 'local -' to make all shell option
changes local to a function. Making IFS local was already trivial on
older bash versions. This allows the following simple reimplementation
of awk split() on bash 5.0+:

function split
{ local -n a=$2
local IFS=$3 -
set -o noglob
a=( $1 )
}

On ksh93 and mksh, shell options are always local for functions declared
with the 'function' keyword, so the *ksh version is even simpler:

function split
{ nameref a=$2
typeset IFS=$3
set -o noglob
a=( $1 )
}

--
|| modernish -- harness the shell
|| https://github.com/modernish/modernish
||
|| KornShell lives!
|| https://github.com/ksh93/ksh

1
server_pubkey.txt

rocksolid light 0.9.7
clearnet tor