zsh-users
 help / color / mirror / code / Atom feed
* field splitting
@ 2020-04-22 16:43 Pier Paolo Grassi
  2020-04-22 16:47 ` Mikael Magnusson
  0 siblings, 1 reply; 11+ messages in thread
From: Pier Paolo Grassi @ 2020-04-22 16:43 UTC (permalink / raw)
  To: Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 753 bytes --]

I am trying to use field splitting to get the first word of a line. I am
using:

echo -n ${${(s: :)line}[1]}
when input contains spaces I get the first word as needed, eg:
line is: /usr/bin/python  - /usr/bin/python /usr/local/bin/terminator
what is printed:  /usr/bin/python

when input does not contains spaces, however, all I get is the first
character of the line, eg:
line is: /usr/bin/zsh
what is printed: /

I have trid forcing the array expansion in various ways:
  ${${(@s: :)line}[1]}
  ${${(s: :)line[@]}[1]}
  ${"${(s: :)line[@]}"[1]}

but I  couldn't get the desidered behaviour
what am I missing?

Pier Paolo Grassi
linkedin: https://www.linkedin.com/in/pier-paolo-grassi-19300217
founder: https://www.meetup.com/it-IT/Machine-Learning-TO

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: field splitting
  2020-04-22 16:43 field splitting Pier Paolo Grassi
@ 2020-04-22 16:47 ` Mikael Magnusson
  2020-04-22 17:11   ` Pier Paolo Grassi
  0 siblings, 1 reply; 11+ messages in thread
From: Mikael Magnusson @ 2020-04-22 16:47 UTC (permalink / raw)
  To: Pier Paolo Grassi; +Cc: Zsh-Users List

On 4/22/20, Pier Paolo Grassi <pierpaolog@gmail.com> wrote:
> I am trying to use field splitting to get the first word of a line. I am
> using:
>
> echo -n ${${(s: :)line}[1]}
> when input contains spaces I get the first word as needed, eg:
> line is: /usr/bin/python  - /usr/bin/python /usr/local/bin/terminator
> what is printed:  /usr/bin/python
>
> when input does not contains spaces, however, all I get is the first
> character of the line, eg:
> line is: /usr/bin/zsh
> what is printed: /
>
> I have trid forcing the array expansion in various ways:
>   ${${(@s: :)line}[1]}
>   ${${(s: :)line[@]}[1]}
>   ${"${(s: :)line[@]}"[1]}
>
> but I  couldn't get the desidered behaviour
> what am I missing?

echo -n ${${(As: :)line}[1]}

-- 
Mikael Magnusson

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: field splitting
  2020-04-22 16:47 ` Mikael Magnusson
@ 2020-04-22 17:11   ` Pier Paolo Grassi
  2020-04-22 17:18     ` Roman Perepelitsa
  0 siblings, 1 reply; 11+ messages in thread
From: Pier Paolo Grassi @ 2020-04-22 17:11 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 1110 bytes --]

doesn't seem to work

line="abcd"
echo -n ${${(As: :)line}[1]}
a

Pier Paolo Grassi
linkedin: https://www.linkedin.com/in/pier-paolo-grassi-19300217
founder: https://www.meetup.com/it-IT/Machine-Learning-TO


Il giorno mer 22 apr 2020 alle ore 18:47 Mikael Magnusson <mikachu@gmail.com>
ha scritto:

> On 4/22/20, Pier Paolo Grassi <pierpaolog@gmail.com> wrote:
> > I am trying to use field splitting to get the first word of a line. I am
> > using:
> >
> > echo -n ${${(s: :)line}[1]}
> > when input contains spaces I get the first word as needed, eg:
> > line is: /usr/bin/python  - /usr/bin/python /usr/local/bin/terminator
> > what is printed:  /usr/bin/python
> >
> > when input does not contains spaces, however, all I get is the first
> > character of the line, eg:
> > line is: /usr/bin/zsh
> > what is printed: /
> >
> > I have trid forcing the array expansion in various ways:
> >   ${${(@s: :)line}[1]}
> >   ${${(s: :)line[@]}[1]}
> >   ${"${(s: :)line[@]}"[1]}
> >
> > but I  couldn't get the desidered behaviour
> > what am I missing?
>
> echo -n ${${(As: :)line}[1]}
>
> --
> Mikael Magnusson
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: field splitting
  2020-04-22 17:11   ` Pier Paolo Grassi
@ 2020-04-22 17:18     ` Roman Perepelitsa
  2020-04-22 17:46       ` Pier Paolo Grassi
  0 siblings, 1 reply; 11+ messages in thread
From: Roman Perepelitsa @ 2020-04-22 17:18 UTC (permalink / raw)
  To: Pier Paolo Grassi; +Cc: Mikael Magnusson, Zsh-Users List

On Wed, Apr 22, 2020 at 7:13 PM Pier Paolo Grassi <pierpaolog@gmail.com> wrote:
>
> doesn't seem to work
>
> line="abcd"
> echo -n ${${(As: :)line}[1]}
> a

Curious. How about this?

  zsh -fc 'line="abcd"; echo ${${(As: :)line}[1]} $ZSH_VERSION'


Roman.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: field splitting
  2020-04-22 17:18     ` Roman Perepelitsa
@ 2020-04-22 17:46       ` Pier Paolo Grassi
  2020-04-22 17:51         ` Roman Perepelitsa
  0 siblings, 1 reply; 11+ messages in thread
From: Pier Paolo Grassi @ 2020-04-22 17:46 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Mikael Magnusson, Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 601 bytes --]

zsh -fc 'line="abcd"; echo ${${(As: :)line}[1]} $ZSH_VERSION'
a 5.1.1

Pier Paolo Grassi
linkedin: https://www.linkedin.com/in/pier-paolo-grassi-19300217
founder: https://www.meetup.com/it-IT/Machine-Learning-TO


Il giorno mer 22 apr 2020 alle ore 19:18 Roman Perepelitsa <
roman.perepelitsa@gmail.com> ha scritto:

> On Wed, Apr 22, 2020 at 7:13 PM Pier Paolo Grassi <pierpaolog@gmail.com>
> wrote:
> >
> > doesn't seem to work
> >
> > line="abcd"
> > echo -n ${${(As: :)line}[1]}
> > a
>
> Curious. How about this?
>
>   zsh -fc 'line="abcd"; echo ${${(As: :)line}[1]} $ZSH_VERSION'
>
>
> Roman.
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: field splitting
  2020-04-22 17:46       ` Pier Paolo Grassi
@ 2020-04-22 17:51         ` Roman Perepelitsa
  2020-04-22 17:53           ` Pier Paolo Grassi
  0 siblings, 1 reply; 11+ messages in thread
From: Roman Perepelitsa @ 2020-04-22 17:51 UTC (permalink / raw)
  To: Pier Paolo Grassi; +Cc: Mikael Magnusson, Zsh-Users List

On Wed, Apr 22, 2020 at 7:47 PM Pier Paolo Grassi <pierpaolog@gmail.com> wrote:
>
> zsh -fc 'line="abcd"; echo ${${(As: :)line}[1]} $ZSH_VERSION'
> a 5.1.1

Time to upgrade.

Roman.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: field splitting
  2020-04-22 17:51         ` Roman Perepelitsa
@ 2020-04-22 17:53           ` Pier Paolo Grassi
  2020-04-22 18:04             ` Roman Perepelitsa
  2020-04-22 18:14             ` Bart Schaefer
  0 siblings, 2 replies; 11+ messages in thread
From: Pier Paolo Grassi @ 2020-04-22 17:53 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Mikael Magnusson, Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 524 bytes --]

so it's a bug in that version? this is the zsh that comes with ubuntu xenial

Pier Paolo Grassi
linkedin: https://www.linkedin.com/in/pier-paolo-grassi-19300217
founder: https://www.meetup.com/it-IT/Machine-Learning-TO


Il giorno mer 22 apr 2020 alle ore 19:51 Roman Perepelitsa <
roman.perepelitsa@gmail.com> ha scritto:

> On Wed, Apr 22, 2020 at 7:47 PM Pier Paolo Grassi <pierpaolog@gmail.com>
> wrote:
> >
> > zsh -fc 'line="abcd"; echo ${${(As: :)line}[1]} $ZSH_VERSION'
> > a 5.1.1
>
> Time to upgrade.
>
> Roman.
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: field splitting
  2020-04-22 17:53           ` Pier Paolo Grassi
@ 2020-04-22 18:04             ` Roman Perepelitsa
  2020-04-22 18:18               ` Pier Paolo Grassi
  2020-04-22 18:14             ` Bart Schaefer
  1 sibling, 1 reply; 11+ messages in thread
From: Roman Perepelitsa @ 2020-04-22 18:04 UTC (permalink / raw)
  To: Pier Paolo Grassi; +Cc: Mikael Magnusson, Zsh-Users List

On Wed, Apr 22, 2020 at 7:53 PM Pier Paolo Grassi <pierpaolog@gmail.com> wrote:
>
> so it's a bug in that version? this is the zsh that comes with ubuntu xenial

I don't know whether it's a bug. I do know that it works in 5.4, 5.7
and 5.8. I haven't tried other versions.

Ubuntu LTS promises to backport only security bug fixes. Very few of
zsh bugs are classified as such. It's not unlikely that zsh 5.1.1 you
are using is the same that was released in 2015 (I haven't actually
checked it). FWIW, zsh that went into Ubuntu 18.04 had a nasty bug
that made err_return option unusable. It still is. This is by design.

Zsh 5.8 -- the latest version -- doesn't have many new features
compared to 5.1 (it does have a few) but the number of bugs that have
been fixed is massive. If you cannot upgrade your distro, you can try
building Zsh from source or use https://github.com/romkatv/zsh-bin
(disclaimer: I maintain this zsh distribution).

Roman.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: field splitting
  2020-04-22 17:53           ` Pier Paolo Grassi
  2020-04-22 18:04             ` Roman Perepelitsa
@ 2020-04-22 18:14             ` Bart Schaefer
  2020-04-22 18:20               ` Pier Paolo Grassi
  1 sibling, 1 reply; 11+ messages in thread
From: Bart Schaefer @ 2020-04-22 18:14 UTC (permalink / raw)
  To: Pier Paolo Grassi; +Cc: Roman Perepelitsa, Mikael Magnusson, Zsh-Users List

On Wed, Apr 22, 2020 at 10:54 AM Pier Paolo Grassi <pierpaolog@gmail.com> wrote:
>
> so it's a bug in that version? this is the zsh that comes with ubuntu xenial

It's intentional behavior; the newer version provides an alternate
behavior that is closer to what you want.

The workaround in older versions of zsh is to use a dummy array
variable:   ${${(As: :)dummy::=$line}[1]}

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: field splitting
  2020-04-22 18:04             ` Roman Perepelitsa
@ 2020-04-22 18:18               ` Pier Paolo Grassi
  0 siblings, 0 replies; 11+ messages in thread
From: Pier Paolo Grassi @ 2020-04-22 18:18 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Mikael Magnusson, Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 1406 bytes --]

yeah saw it in an earlier post, very nice. for my personal enjoyment I
tried it, but for prod env changing one thing usually lead to changing a
lot or at least to testing a lot, so I will go with an intermediate
assignment for now to preserve the field splitting in an array
thanks for your time


Il giorno mer 22 apr 2020 alle 20:04 Roman Perepelitsa <
roman.perepelitsa@gmail.com> ha scritto:

> On Wed, Apr 22, 2020 at 7:53 PM Pier Paolo Grassi <pierpaolog@gmail.com>
> wrote:
> >
> > so it's a bug in that version? this is the zsh that comes with ubuntu
> xenial
>
> I don't know whether it's a bug. I do know that it works in 5.4, 5.7
> and 5.8. I haven't tried other versions.
>
> Ubuntu LTS promises to backport only security bug fixes. Very few of
> zsh bugs are classified as such. It's not unlikely that zsh 5.1.1 you
> are using is the same that was released in 2015 (I haven't actually
> checked it). FWIW, zsh that went into Ubuntu 18.04 had a nasty bug
> that made err_return option unusable. It still is. This is by design.
>
> Zsh 5.8 -- the latest version -- doesn't have many new features
> compared to 5.1 (it does have a few) but the number of bugs that have
> been fixed is massive. If you cannot upgrade your distro, you can try
> building Zsh from source or use https://github.com/romkatv/zsh-bin
> (disclaimer: I maintain this zsh distribution).
>
> Roman.
>
-- 
Pier Paolo Grassi

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: field splitting
  2020-04-22 18:14             ` Bart Schaefer
@ 2020-04-22 18:20               ` Pier Paolo Grassi
  0 siblings, 0 replies; 11+ messages in thread
From: Pier Paolo Grassi @ 2020-04-22 18:20 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Mikael Magnusson, Roman Perepelitsa, Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 550 bytes --]

thanks will go with this for now

Il giorno mer 22 apr 2020 alle 20:14 Bart Schaefer <
schaefer@brasslantern.com> ha scritto:

> On Wed, Apr 22, 2020 at 10:54 AM Pier Paolo Grassi <pierpaolog@gmail.com>
> wrote:
> >
> > so it's a bug in that version? this is the zsh that comes with ubuntu
> xenial
>
> It's intentional behavior; the newer version provides an alternate
> behavior that is closer to what you want.
>
> The workaround in older versions of zsh is to use a dummy array
> variable:   ${${(As: :)dummy::=$line}[1]}
>
-- 
Pier Paolo Grassi

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-04-22 18:20 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-22 16:43 field splitting Pier Paolo Grassi
2020-04-22 16:47 ` Mikael Magnusson
2020-04-22 17:11   ` Pier Paolo Grassi
2020-04-22 17:18     ` Roman Perepelitsa
2020-04-22 17:46       ` Pier Paolo Grassi
2020-04-22 17:51         ` Roman Perepelitsa
2020-04-22 17:53           ` Pier Paolo Grassi
2020-04-22 18:04             ` Roman Perepelitsa
2020-04-22 18:18               ` Pier Paolo Grassi
2020-04-22 18:14             ` Bart Schaefer
2020-04-22 18:20               ` Pier Paolo Grassi

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).