[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 03:52:01PM +0200, Thomas Schmitt wrote:
> I tried
> 
>   echo " 34 + 45 \| abc \| 1 2 3 \| c\|123abc " | \
>   sed -e 's/\\|/\n/g' | \
>   mapfile -t _S_AR
> 
> with no visible effect on the array _S_AR. bash 4.3.30 and 5.0.11.

You can't use a pipeline, because each command in the pipeline runs in
a separate subshell.

> This works
> 
>   echo " 34 + 45 \| abc \| 1 2 3 \| c\|123abc " | sed -e 's/\\|/\n/g' >/tmp/x
> 
>   mapfile -t _S_AR < /tmp/x

Yes.  Temp files are fine.

> A workaround without temporary file would be
> 
>   x=" 34 + 45 \| abc \| 1 2 3 \| c\|123abc "
>   split=$(echo "$x" | sed -e 's/\\|/\n/g')
>   end=$(echo -n "$x" | sed -e 's/./+/g')"+"
> 
>   mapfile -t _S_AR <<$end
>   $split
>   $end

No... just use the process substitution that I showed you already.

mapfile -t myarray < <(some command)

Do you need an actual demonstration?

unicorn:~$ f() { printf '%s\n' ' one ' 'two' 't h r e e'; }
unicorn:~$ mapfile -t a < <(f)
unicorn:~$ declare -p a
declare -a a=([0]=" one " [1]="two" [2]="t h r e e")


Reply to: