Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Array parameter expansion: take n-th word from array elements
- X-seq: zsh-users 22750
- From: "Nikolay Aleksandrovich Pavlov (ZyX)" <kp-pav@xxxxxxxxx>
- To: Alexander Groß <agross@xxxxxxxxxxxxxxxx>, "zsh-users@xxxxxxx" <zsh-users@xxxxxxx>
- Subject: Re: Array parameter expansion: take n-th word from array elements
- Date: Sun, 25 Jun 2017 20:34:24 +0300
- Authentication-results: mxback3h.mail.yandex.net; dkim=pass header.i=@yandex.ru
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1498412064; bh=jkSdLli6cUgGBl/xpnwGwhW4DBZVpJ1Vm9WTKEi4PiY=; h=From:To:In-Reply-To:References:Subject:Message-Id:Date; b=ZKfJ+1CaULzP44myTXe+LQ4iXtZWppq4yVA3rORPZNpZbuBsu+dY59RfPdOpfIZYo S9lKGMzeY0YWMn0a/kJf4oXdaK2px8QzUh7korMRA0E/8sW/QLkPe5HpupAl3lXkg7 tkpiTFfEh9kKXQSwsMQ8CMbiIaLnpUOioZzE3l6o=
- In-reply-to: <CACcHa=NNdnCAb8YcQkLqnXbQXt5-ttM5=MM=b5kdmUVcWhiuQQ@mail.gmail.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CACcHa=NNdnCAb8YcQkLqnXbQXt5-ttM5=MM=b5kdmUVcWhiuQQ@mail.gmail.com>
25.06.2017, 15:46, "Alexander Groß" <agross@xxxxxxxxxxxxxxxx>:
> Hi all,
>
> I would like to parse some output from a command that generates an array of
> the following elements:
>
> Main System.Xml.XPath - 4.3
> Main System.Xml.XPath.XDocument - 4.3
> Test Castle.Core - 4.0
> Test FsCheck - 2.9
>
> E.g.
>
> foo=('a interesting - b' 'a also-interesting - c')
>
> I would like to take the second word from each and every of the array
> elements (interesting also-interesting). Unfortunately,
>
> print -l ${foo[2]}
>
> prints the second element, 'a also-interesting - c'.
>
> A for loop works:
>
> local -a filtered
> for package in $foo; do
> # Split by space and take second word.
> filtered+="${${(s. .)package}[2]}"
> done
>
> But I wonder if there is a better way using parameter expansion. Here's
> what I came up with so far:
> print -l ${${(s. .)foo}[2]}
>
> Unfortunately, it just prints 'interesting'.
>
> Any ideas pointing me in the right direction are greatly appreciated!
I would just go with ${#} and ${%}:
% foo=('a interesting - b' 'a also-interesting - c')
% echo ${${foo% - *}#* }
interesting also-interesting
>
> Alex
> --
> Alexander Groß
> http://therightstuff.de/
Messages sorted by:
Reverse Date,
Date,
Thread,
Author