Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.debian.user > #225922
| From | John Crawley <john@bunsenlabs.org> |
|---|---|
| Newsgroups | linux.debian.user |
| Subject | Re: delimiters with more than one character? ... |
| Date | 2020-08-06 05:00 +0200 |
| Message-ID | <AAEil-4Rk-3@gated-at.bofh.it> (permalink) |
| References | <AssHn-5be-3@gated-at.bofh.it> <AAl61-1NF-15@gated-at.bofh.it> <AArl8-5DY-17@gated-at.bofh.it> |
| Organization | BunsenLabs |
On 2020-08-05 22:06, Greg Wooledge wrote:
> On Wed, Aug 05, 2020 at 03:04:32PM +0900, John Crawley wrote:
>> This method cuts off the first part of the string, up to the delimiter, and
>> adds it to the array, then continues with what's left of the string until
>> there's none left:
>
> Yes, that's a valid approach, although not one that most bash scripters
> would choose, because it's hideously slow in bash, and doesn't "feel"
> like it's in the "spirit" of shell scripting.
I likely don't have a sufficient grasp of that "spirit" to comment there :)
but as for "hideously slow", remain to be convinced. Bash string
modifications seem to be pretty fast. Would very long strings requiring
multiple runs of the loop slow it down excessively? (See below for
stringx1024) With the OP's provided string, on my system:
time bash ./multi-delim.sh
declare -a arr=([0]=" 34 + 45 " [1]=" abc " [2]=" 1 2 3 " [3]=" c"
[4]="123abc ")
real 0m0.004s
user 0m0.000s
sys 0m0.000s
Exact time varies from 1ms to 5ms, but it seems faster than python, perl
or a bash script calling sed or awk would be.
> Your output doesn't match your input. You've got an extra backslash
> character in the [1] element. Perhaps you tested with several different
> inputs, and accidentally pasted the wrong output.
Thanks - that was indeed the case. Sorry.
( Tested with some other characters like * # ! Everything seems to be
treated as a plain string, needing no escaping. )
>> I've used this myself, so am eager to hear of any hidden snags. :)
>>
>> (One already: if the delimiter is a repeated character which might also be
>> the last in the last string fragment, then the loop never closes. Fairly
>> rare?)
>
> OK... yeah, that would be a show-stopper, all right. I was able
> to reproduce the infinite loop...
(snip)
> A different approach is needed. The one that immediately springs to
> mind for me is:
>
> s='a || b || c ||| d |'
> del='||'
> arr=()
> while [[ $s = *"$del"* ]]; do arr+=( "${s%%"$del"*}" ); s=${s#*"$del"}; done
> arr+=( "$s" )
>
> This is very similar to the flawed approach, but instead of appending
> an extra delimiter to the input and looping until the input is empty,
> we *check* for the existence of a delimiter in the input, and loop until
> none is found. Whatever is left over becomes the final array element
> This will be a little bit slower than the flawed code (probably), but I
> believe it is free from infinite loops.
Nice! The run time is very similar.
Adding this:
for i in {1..10}
do
_S+=$del$_S
done
to multipy the string by 1024 gave times of ~3.9s and ~4.0s.
I can't see any way the loop would fail to end. This will be my
boilerplate if I ever need to do such a thing again...
> Since we're not modifying the input by appending an extra delimiter,
> any ambiguity in the input string was put there by the original input
> source, not created by our code. In my sample input, the ||| substring
> between c and d can be parsed as either "delimiter followed by pipe",
> or "pipe followed by delimiter". It is impossible to tell which one
> is correct, because we have no external knowledge of the input string.
> Therefore, either result must be acceptable. If the humans who
> provided this input don't care for the results they get, well, it's
> their problem and not ours.
I guess there's little need to try to cope with delimiters that would be
ambiguous in practice...
--
John
Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
delimiters with more than one character? ... Albretch Mueller <lbrtchx@gmail.com> - 2020-07-14 15:00 +0200
Re: delimiters with more than one character? ... Alex Mestiashvili <amestia@rsh2.donotuse.de> - 2020-07-14 15:00 +0200
Re: delimiters with more than one character? ... Greg Wooledge <wooledg@eeg.ccf.org> - 2020-07-14 15:10 +0200
Re: delimiters with more than one character? ... Nicolas George <george@nsup.org> - 2020-07-14 15:10 +0200
Re: delimiters with more than one character? ... Alex Mestiashvili <amestia@rsh2.donotuse.de> - 2020-07-14 15:20 +0200
Re: delimiters with more than one character? ... Greg Wooledge <wooledg@eeg.ccf.org> - 2020-07-14 15:10 +0200
Re: delimiters with more than one character? ... Nicolas George <george@nsup.org> - 2020-07-14 15:20 +0200
Re: delimiters with more than one character? ... Nicolas George <george@nsup.org> - 2020-07-14 17:30 +0200
Re: delimiters with more than one character? ... Albretch Mueller <lbrtchx@gmail.com> - 2020-07-15 12:30 +0200
Re: delimiters with more than one character? ... Greg Wooledge <wooledg@eeg.ccf.org> - 2020-07-15 14:00 +0200
Re: delimiters with more than one character? ... David Wright <deblis@lionunicorn.co.uk> - 2020-07-15 14:30 +0200
Re: delimiters with more than one character? ... Bob Weber <bobrweber@gmail.com> - 2020-07-15 14:40 +0200
Re: delimiters with more than one character? ... Greg Wooledge <wooledg@eeg.ccf.org> - 2020-07-15 14:50 +0200
Re: delimiters with more than one character? ... Bob Weber <bobrweber@gmail.com> - 2020-07-15 16:20 +0200
Re: delimiters with more than one character? ... Greg Wooledge <wooledg@eeg.ccf.org> - 2020-07-15 16:30 +0200
Re: delimiters with more than one character? ... The Wanderer <wanderer@fastmail.fm> - 2020-07-15 16:30 +0200
Re: delimiters with more than one character? ... Nicolas George <george@nsup.org> - 2020-07-15 17:50 +0200
Re: delimiters with more than one character? ... Nicolas George <george@nsup.org> - 2020-07-15 15:10 +0200
Re: delimiters with more than one character? ... Bob Weber <bobrweber@gmail.com> - 2020-07-14 17:30 +0200
Re: delimiters with more than one character? ... Alessandro Baggi <alessandro.baggi@gmail.com> - 2020-07-15 17:30 +0200
Re: delimiters with more than one character? ... Eric S Fraga <e.fraga@ucl.ac.uk> - 2020-07-16 12:10 +0200
Re: delimiters with more than one character? ... "Thomas Schmitt" <scdbackup@gmx.net> - 2020-07-16 13:40 +0200
Re: delimiters with more than one character? ... Greg Wooledge <wooledg@eeg.ccf.org> - 2020-07-16 14:00 +0200
Re: delimiters with more than one character? ... Greg Wooledge <wooledg@eeg.ccf.org> - 2020-07-16 16:00 +0200
Re: delimiters with more than one character? ... Zenaan Harkness <zenaan@freedbms.net> - 2020-07-17 04:50 +0200
Re: delimiters with more than one character? ... Nicolas George <george@nsup.org> - 2020-07-17 10:50 +0200
Re: delimiters with more than one character? ... rhkramer@gmail.com - 2020-07-17 13:20 +0200
tmpfs is not a ramdisk (was: delimiters with more than one character? ...) Nicolas George <george@nsup.org> - 2020-07-17 13:30 +0200
Re: tmpfs is not a ramdisk (was: delimiters with more than one character? ...) David Wright <deblis@lionunicorn.co.uk> - 2020-07-17 18:20 +0200
Re: tmpfs is not a ramdisk (was: delimiters with more than one character? ...) rhkramer@gmail.com - 2020-07-17 20:30 +0200
Re: tmpfs is not a ramdisk (was: delimiters with more than one character? ...) Zenaan Harkness <zenaan@freedbms.net> - 2020-07-18 07:20 +0200
Re: tmpfs is not a ramdisk (was: delimiters with more than one character? ...) <tomas@tuxteam.de> - 2020-07-18 10:40 +0200
Re: delimiters with more than one character? ... Greg Wooledge <wooledg@eeg.ccf.org> - 2020-07-17 13:40 +0200
Re: delimiters with more than one character? ... "Thomas Schmitt" <scdbackup@gmx.net> - 2020-07-16 16:00 +0200
Re: delimiters with more than one character? ... Zenaan Harkness <zenaan@freedbms.net> - 2020-07-17 04:50 +0200
Re: delimiters with more than one character? ... Eric S Fraga <e.fraga@ucl.ac.uk> - 2020-07-16 15:00 +0200
Re: delimiters with more than one character? ... John Crawley <john@bunsenlabs.org> - 2020-08-05 08:30 +0200
Re: delimiters with more than one character? ... Greg Wooledge <wooledg@eeg.ccf.org> - 2020-08-05 15:10 +0200
Re: delimiters with more than one character? ... John Crawley <john@bunsenlabs.org> - 2020-08-06 05:00 +0200
Re: delimiters with more than one character? ... Greg Wooledge <wooledg@eeg.ccf.org> - 2020-08-06 13:40 +0200
Re: delimiters with more than one character? ... John Crawley <john@bunsenlabs.org> - 2020-08-07 03:10 +0200
csiph-web