[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 Mon, Feb 20, 2023 at 07:24:01PM +0000, Albretch Mueller wrote:
> > https://mywiki.wooledge.org/BashPitfalls#pf47
> >
>  what I am trying to do is split a string using as delimiter a pipe

The web page you cited tells you how, doesn't it?  Assuming your string
is a line (e.g. something you pulled out of a *simplified* CSV file,
where there are no delimiters inside fields), and that you want to store
the fields in an array, you can simply do:

IFS="|" read -ra myarray <<< "$mystring|"

Demonstration:

unicorn:~$ mystring='foo|bar|last|field|is|empty|'
unicorn:~$ IFS="|" read -ra myarray <<< "$mystring|"
unicorn:~$ declare -p myarray
declare -a myarray=([0]="foo" [1]="bar" [2]="last" [3]="field" [4]="is" [5]="empty" [6]="")

> I used to do that with awk,

I don't understand how awk helps you populate the elements of a bash
array.  Awk can write a new string to stdout, but then you still have
to parse that string in bash...?  I don't see what benefit awk gives
you here.

>  How do you split a string using as delimiter a pipe these days
> without using a bloody hack?

You cited a bash web page.  So, everything you're doing is a hack.
That's the nature of bash.


Reply to: