help-bash
[Top][All Lists]
Advanced

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

Re: String to array


From: Tapani Tarvainen
Subject: Re: String to array
Date: Thu, 30 Sep 2021 17:19:21 +0300

On Thu, Sep 30, 2021 at 03:49:04PM +0200, Alex fxmbsw7 Ratchev 
(fxmbsw7@gmail.com) wrote:

> at a second try with awk, the spawn awk once per str works, but the coproc
> doesnt
> im not sure why maybe someone knows
> 
> ive choosen awk for speed
> s=ab34 ; awksplit() { gawk -v FS= -v RS=\\0 -v ORS=\\0 -e '{ gsub( /./,
> "\\\\& " ) ; print }' "$@" ; } ; declare -a "one=( $( awksplit <<<"$s" -v
> ORS= ) )" ; declare -p one  ; [[ ! -v ser ]] && coproc ser { awksplit ; } ;
> ser() { printf %s\\0 "${@:2}" >&${ser[1]} ; declare -ga "$1=( $( <
> /proc/fd/$ser ) )" ; declare -p $1 ; } ; ser serial "$s"
> 
> declare -a one=([0]="a" [1]="b" [2]="3" [3]="4")

That is... perhaps a bit over-complicated, and awk is no better
than sed here.

Anyway, here's a bit simpler awk solution:

array=($(awk -vFPAT=. '{for(i=1;i<=NF;i++)print "\""$i"\""}' <<<$string))

It does fail with some special characters, however.

-- 
Tapani Tarvainen



reply via email to

[Prev in Thread] Current Thread [Next in Thread]