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

Re: awk not just using the Field separator as such. it is using the blank space as well ...



On Wed, Feb 15, 2023 at 12:09:28PM +0000, Albretch Mueller wrote:
> On 2/15/23, DdB <debianlist@potentially-spam.de-bruyn.de> wrote:
> > $ echo "Adams, Fred, and Ken Aizawa \"The Bounds of Cognition\"" | awk
> > -F'\"' '{for (i=1; i<=NF; i++) print $i;}'
> > Adams, Fred, and Ken Aizawa
> > The Bounds of Cognition
> 
>  yes and this also works:
> 
> _L="Adams, Fred, and Ken Aizawa \"The Bounds of Cognition\""
> echo "${_L}" | awk -F'\"' '{for (i=1; i<=NF; i++) print $i;}'
> Adams, Fred, and Ken Aizawa
> The Bounds of Cognition
> 
>  but I wasn't able to write the output into an array

If you want to read LINES of a STREAM as array elements, use mapfile:

mapfile -t myarray < <(
    printf '%s\n' "$stuff" | awk -F'\"' '...'
)

If you want to read FIELDS of a SINGLE LINE as array elements, use
read -ra:

read -ra myarray <<< "$one_line"

Note the caveats associated with each of these, especially the second
one.  Very few things in bash ever work as you expect once you start
poking at the corner cases.

https://mywiki.wooledge.org/BashPitfalls#pf47


Reply to: