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

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



On Thu, Jul 16, 2020 at 01:30:02PM +0200, Thomas Schmitt wrote:
> Hi,
> 
> Eric S Fraga wrote:
> > echo " 34 + 45 \| abc \| 1 2 3 \| c\|123abc " | sed -e 's/\\|/\n/g'
> 
> I came that far too. One can wrap the result in quotation marks by adding
>   | sed -e 's/^.*$/"&"/'
> 
> But how to get the lines into the array ?

First, do not add quotation marks to it.  They just get in the way.

If you've got an input stream with one element per line that you want
to import into a bash array, you use the "mapfile" (or "readarray")
command.

mapfile -t myarray < <(blah | sed)

If it's a NUL-delimited stream, and you're in bash 4.4 or higher, you
can use mapfile -d '' myarray < <(...) instead.

If it's a NUL-delimited stream but you're in bash 4.3 or older, then
you have to use a loop.

myarray=()
while IFS= read -r -d '' i; do
  myarray+=("$i")
done < <(...)


Reply to: