[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: delimiters with more than one character? ...



On 2020-08-06 20:33, Greg Wooledge wrote:
For short strings, doing this sort of parsing in bash is fine.  But as
you can see, on large inputs, it does not scale well.  The fact that each
iteration makes a *copy* of nearly the entire remaining input doesn't
help matters, either -- right off the bat, that algorithm makes the run
time explode, even before you take into account bash's 100% interpreted
runtime slowness.

Thanks for the insights.
So, re: the OP's question, it's possible in bash, but there's unlikely to be an efficient way for long strings.

But (unwilling to give up) IF it can be guaranteed that the string will never contain newlines, then this is about 16x faster than the snip-and-loop version:
---
#!/bin/bash

_S=' 34 + 45 \| abc \| 1 2 3 \| c\|123abc '
del='\|'

mapfile -t arr <<< ${_S//"${del}"/$'\n'}

declare -p arr
---
Seems to accept unusual characters and empty items.
Awk can ignore newlines, but a lot of the other shell tools are line-based anyway.

--
John


Reply to: